How to pass variables to string mixins?
Benjamin Schaaf
ben.schaaf at gmail.com
Tue Feb 26 01:06:22 UTC 2019
On Tuesday, 26 February 2019 at 00:07:54 UTC, Victor Porton wrote:
> I want to create a string mixin based on a supplementary
> variable (name2 below):
>
> Let we have something like:
>
> mixin template X(string name) {
> immutable string name2 = '_' ~ name;
> mixin("struct " ~ name2 ~ "{ int i; }");
> }
>
> But it would create variable name2 inside X, which should not
> be created. How to solve this problem?
Your best bet is to just not declare anything in a mixin template
that you don't want included:
mixin template X(string name) {
mixin("struct _" ~ name ~ "{ int i; }");
}
Or don't use mixin templates:
template X(string name) {
immutable string name2 = '_' ~ name;
enum X = "struct " ~ name2 ~ "{ int i; }";
}
mixin(X!("foo"));
More information about the Digitalmars-d-learn
mailing list