www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixin statements

reply "Tofu Ninja" <emmons0 purdue.edu> writes:
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?
Jun 11 2015
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
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);
Jun 12 2015
parent reply Jacob Carlborg <doob me.com> writes:
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
Jun 12 2015
parent "Tofu Ninja" <emmons0 purdue.edu> writes:
On Friday, 12 June 2015 at 14:22:45 UTC, Jacob Carlborg wrote:
 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.
But without the AST ;) never actually have to deal with ast's but still has a lot of its functionality in a simpler form.
Jun 12 2015