How to pass variables to string mixins?

Rubn where at is.this
Tue Feb 26 01:02:17 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?

Probably the easiest way is just to move it into another function 
outside so that it doesn't get mixed in with the mixin template.

  private string buildStructString(string name2) {
     return "struct " ~ name2 ~ "{ int i; }"
  }

  mixin template X(string name) {
    mixin(buildStructString("_" ~ name));
  }


More information about the Digitalmars-d-learn mailing list