Mixin statements

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 12 01:06:57 PDT 2015


Also a feature that would make this perfect is macro template 
arguments.

Something like

void templateFunction(macro a)()
{
      int x = 5;
      return mixin(a);
}

Essentially, the expression in the argument would be converted to 
an expression macro.

Calling templateFunction!(x + x)() would return 10

Why would you want this over string mixins?
For one, it looks a lot cleaner.
Also as its not a string, it cant be changed and the parser need 
not re-parse it when it gets mixed in, which could be faster than 
normal string mixins.
Also it would allow for mixin macros to look something similar to 
c++ macros only with the mixin keyword in front of it. Would keep 
the whole thing clean looking.

Example assert doing c style return error codes.

mixin macro cassert(macro expression, alias errorcode)
{
      if(!(mixin(expression))) return errorcode;
}

... some other piece of code that returns null on error ...
mixin cassert(x == 3, null);



More information about the Digitalmars-d mailing list