System programming in D (Was: The God Language)

Timon Gehr timon.gehr at gmx.ch
Fri Dec 30 13:51:53 PST 2011


On 12/30/2011 09:51 PM, Andrei Alexandrescu wrote:
> On 12/30/11 12:10 PM, Walter Bright wrote:
>> On 12/30/2011 4:05 AM, Timon Gehr wrote:
>>> It certainly does. That is how all my code generation looks like. The
>>> fact that
>>> I am using string mixins to solve some problems shows that those are not
>>> 'problems in D string mixins'.
>>
>> I your solution to parameterized strings is very nice. Can you write a
>> brief article about it? This should be more widely known.
>
> The idea is good, but nonhygienic: the macro's expansion picks up
> symbols from the expansion context.
>

What the template 'X' currently achieves is an improvement in syntax:

string generated = "foo!\""~x~"\"(\""~bar(y)~"\")";

vs

string generated = mixin(X!q{
    foo!"@(x)"("@(bar(y))")
});

i.e. it is assumed that the generated code that results in a string 
expression will be mixed in right away. Kenji Hara's string mixin 
template proposal could be pulled to be able to enforce this at the same 
time as improving the syntax further:

mixin template X(string s){enum X = XImpl(s);}

string generated = X!q{
     foo!"@(x)"("@(bar(y))")
}



> Timon, to move from good to great, you may want to add parameters to the
> expansion process such that you replace the argument values during
> expansion.
>


I like this:

string QUX(string param1, string op, string param2){
     return mixin(X!q{
         @("__"~param1) @(op) @(param2~"__");
     };
}

a lot more than this:

string QUX(string param1, string op, string param2){
     return mixin(X!(q{
         @1 @2 @3
     },"__"~param1, op, param2~"__"));
}

In an ideal world, I think the macro could be defined like this (using 
the new anonymous function syntax on a named function):

string QUX(string param1, string op, string param2) => X!q{
     @("__"~param1) @(op) @(param2~"__");
};

and expanded like this:

mixin(QUX("foo","+","bar"));


I think what you have in mind is that macros are defined similar to this:

enum QUX = q{@("__"~#1) @2 @(#3~"__")};

And then would be expanded like this

mixin(X!(QUX, "foo", "+", "bar"));


Is this better? I think it makes it more difficult to write and use such 
a macro, because there are no parameter names to document what the 
parameters are for.









More information about the Digitalmars-d mailing list