Mixin statements

Tofu Ninja via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 11 23:18:43 PDT 2015


Can we get a way to mix in statements and expressions? Current 
mixin templates only allow mixing in declarations. The only way 
to actually mixin statements is through string mixins which are 
not parsable by auto complete and such, and are really over kill 
for a lot of common uses.

My first thought was to just allow statements in a mixin template 
but that does not really make sense. Where would the statements 
be placed for

mixin someMixInTemplateName!() A;

So I propose using the currently unused macro keyword for it(I 
don't think it will ever be used for AST macros any ways...)

mixin macro macroTemplateName(ARGS...)
{
     statements or declarations to be mixed in.
}

and could be use in the same way as mixin templates, just without 
giving it a name. Example...

mixin macroTemplateName!();


Also an extension of this would be to allow expression macros. I 
haven't really thought of a good syntax for defining these 
though, but maybe something like...

mixin macro expressionMacroName() = (some expression to be mixed 
in);

I am not totally sure on that though because I feel like you 
would want to be able to do static if's and such, but you could 
probably at that point call out to a more complex template to 
handle things like that. The only purpose of the expression macro 
would be to allow it to capture identifiers in the calling scope.

Could be used like string mixins. Example...

int a = 3 + 5 + mixin(expressionMacroName!());

Thoughts? Is this something that any one else has wanted?


More information about the Digitalmars-d mailing list