Mixin statements

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 12 07:23:25 PDT 2015


On 2015-06-12 10:06, Tofu Ninja wrote:
> 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);

This starts to look like AST macros to me.


-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list