www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Token strings as "cheap" macros.

reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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
parent reply Reiner Pope <some address.com> writes:
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-Sauls
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(). -- Reiner
Sep 06 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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().
 
   -- Reiner
 
I've made one too :) http://while-nan.blogspot.com/2007/06/mixins-ctfe-and-shell-style-variable.html -- Daniel
Sep 07 2007
parent reply Reiner Pope <some address.com> writes:
Daniel Keep wrote:
 
 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().

   -- Reiner
I've made one too :) http://while-nan.blogspot.com/2007/06/mixins-ctfe-and-shell-style-variable.html -- Daniel
What, the formatting can be evaluated at compile-time? -- Reiner
Sep 07 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Reiner Pope wrote:
 Daniel Keep wrote:
 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().

   -- Reiner
I've made one too :) http://while-nan.blogspot.com/2007/06/mixins-ctfe-and-shell-style-variable.html -- Daniel
What, the formatting can be evaluated at compile-time? -- Reiner
*double take* Ok, don't mind me. I didn't notice the "static" part. /baka /gomen -- Daniel
Sep 07 2007