Inline mixin
Daniel Kozak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Mar 14 11:36:26 PDT 2017
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);
}
More information about the Digitalmars-d-learn
mailing list