digitalmars.D - Token strings as "cheap" macros.
- Chris Nicholson-Sauls (33/33) Sep 06 2007 Looking at D/2.x Token Strings (q{...}) something came to mind. If we h...
- Reiner Pope (17/58) Sep 06 2007 I made my own compile-time formatter. I made one based on Phobos
- Daniel Keep (4/26) Sep 07 2007 I've made one too :)
- Reiner Pope (3/33) Sep 07 2007 What, the formatting can be evaluated at compile-time?
- Daniel Keep (6/42) Sep 07 2007 *double take*
Looking at D/2.x Token Strings (q{...}) something came to mind. If we had a way of embedding expressions which evaluate to strings within this syntax, it could be used to write rather clean mixins. (The use of which I assume to be a driver behind their introduction anyhow.) What I'm thinking of, is this sort of thing: template Gettor (T, char[] name) { const Gettor = T.stringof~" get"~name~" () { return _"~name~"; } "; } Looking more like this: template Gettor (T, char[] name) { const Gettor = q{ ${T.stringof} get${name} () { return _${name}; } }; } Which benefits from being easier to visually parse (and therefore develop and maintain), and being open to editor highlighting. I use `` strings right now, with `~...~` embeds, to similar effect only because my main editor doesn't know about `` strings. Its really just a somewhat limited builtin compile-time-string-formatting capability. The syntax doesn't matter, I just borrowed ${} from PHP. function Gettor ($name) { return <<<EOI function get${name} () { return _${name}; } EOI; } -- Chris Nicholson-Sauls
Sep 06 2007
Chris Nicholson-Sauls wrote:Looking at D/2.x Token Strings (q{...}) something came to mind. If we had a way of embedding expressions which evaluate to strings within this syntax, it could be used to write rather clean mixins. (The use of which I assume to be a driver behind their introduction anyhow.) What I'm thinking of, is this sort of thing: template Gettor (T, char[] name) { const Gettor = T.stringof~" get"~name~" () { return _"~name~"; } "; } Looking more like this: template Gettor (T, char[] name) { const Gettor = q{ ${T.stringof} get${name} () { return _${name}; } }; } Which benefits from being easier to visually parse (and therefore develop and maintain), and being open to editor highlighting. I use `` strings right now, with `~...~` embeds, to similar effect only because my main editor doesn't know about `` strings. Its really just a somewhat limited builtin compile-time-string-formatting capability. The syntax doesn't matter, I just borrowed ${} from PHP. function Gettor ($name) { return <<<EOI function get${name} () { return _${name}; } EOI; } -- Chris Nicholson-SaulsI made my own compile-time formatter. I made one based on Phobos formatting, and then I decided I preferred indexable style (like Tango), so I've got one of each: formatStringTango and formatStringPhobos. Just now, I added a wrapper function for formatStringTango which allows expressions instead of indexes. Here's the unittest: unittest { const x = 5; const y = 7; static assert(mixin(format("{x} = 5, {y} = 7")) == "5 = 5, 7 = 7"); } I've attached the source code. It's full of other templates I made to help work around problems with metaprogramming (__traits in particular), but it at least shows that it's pretty much achievable within the language. Of course, macros would be nice, to remove the mixin(). -- Reiner
Sep 06 2007
Reiner Pope wrote:[snip] I made my own compile-time formatter. I made one based on Phobos formatting, and then I decided I preferred indexable style (like Tango), so I've got one of each: formatStringTango and formatStringPhobos. Just now, I added a wrapper function for formatStringTango which allows expressions instead of indexes. Here's the unittest: unittest { const x = 5; const y = 7; static assert(mixin(format("{x} = 5, {y} = 7")) == "5 = 5, 7 = 7"); } I've attached the source code. It's full of other templates I made to help work around problems with metaprogramming (__traits in particular), but it at least shows that it's pretty much achievable within the language. Of course, macros would be nice, to remove the mixin(). -- ReinerI've made one too :) http://while-nan.blogspot.com/2007/06/mixins-ctfe-and-shell-style-variable.html -- Daniel
Sep 07 2007
Daniel Keep wrote:Reiner Pope wrote:What, the formatting can be evaluated at compile-time? -- Reiner[snip] I made my own compile-time formatter. I made one based on Phobos formatting, and then I decided I preferred indexable style (like Tango), so I've got one of each: formatStringTango and formatStringPhobos. Just now, I added a wrapper function for formatStringTango which allows expressions instead of indexes. Here's the unittest: unittest { const x = 5; const y = 7; static assert(mixin(format("{x} = 5, {y} = 7")) == "5 = 5, 7 = 7"); } I've attached the source code. It's full of other templates I made to help work around problems with metaprogramming (__traits in particular), but it at least shows that it's pretty much achievable within the language. Of course, macros would be nice, to remove the mixin(). -- ReinerI've made one too :) http://while-nan.blogspot.com/2007/06/mixins-ctfe-and-shell-style-variable.html -- Daniel
Sep 07 2007
Reiner Pope wrote:Daniel Keep wrote:*double take* Ok, don't mind me. I didn't notice the "static" part. /baka /gomen -- DanielReiner Pope wrote:What, the formatting can be evaluated at compile-time? -- Reiner[snip] I made my own compile-time formatter. I made one based on Phobos formatting, and then I decided I preferred indexable style (like Tango), so I've got one of each: formatStringTango and formatStringPhobos. Just now, I added a wrapper function for formatStringTango which allows expressions instead of indexes. Here's the unittest: unittest { const x = 5; const y = 7; static assert(mixin(format("{x} = 5, {y} = 7")) == "5 = 5, 7 = 7"); } I've attached the source code. It's full of other templates I made to help work around problems with metaprogramming (__traits in particular), but it at least shows that it's pretty much achievable within the language. Of course, macros would be nice, to remove the mixin(). -- ReinerI've made one too :) http://while-nan.blogspot.com/2007/06/mixins-ctfe-and-shell-style-variable.html -- Daniel
Sep 07 2007