www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inline mixin

reply Inquie <Inquie data1.com> writes:
Many times I need to build a type using a string. I have to 
resort to building the entire expression/statement using the 
mixin:

mixin("This is a long and complex expression where I only need to 
modify X")

Is there any way to do a sort of "inline mixin"?


This is a long and complex expression where I only need to modify 
mixin("X")

?

e.g.,

mixin("int "~name~" = 3;")

vs

int mixin(name) = 3;

(a gross simplification)



Maybe a template could handle it or is something like this 
impossible by D's gramar?
Mar 14
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne 14.3.2017 v 18:38 Inquie via Digitalmars-d-learn napsal(a):

 Many times I need to build a type using a string. I have to resort to 
 building the entire expression/statement using the mixin:

 mixin("This is a long and complex expression where I only need to 
 modify X")

 Is there any way to do a sort of "inline mixin"?


 This is a long and complex expression where I only need to modify 
 mixin("X")

 ?

 e.g.,

 mixin("int "~name~" = 3;")

 vs

 int mixin(name) = 3;

 (a gross simplification)



 Maybe a template could handle it or is something like this impossible 
 by D's gramar?
You can combine template and mixin. something like this: enum Z(alias X) = "int x = " ~ X.stringof ~ ";"; void main() { import std.stdio : writeln; mixin(Z!1); writeln(x); }
Mar 14