String mixin from within a template

data pulverizer data.pulverizer at gmail.com
Sun Aug 23 19:42:47 UTC 2020


Hi all,

I am trying to dynamically create compile time variables by using 
string and template mixins. I have tried both approaches and they 
have failed. The central thing I am trying to create is a line 
like this:

```d
`alias x` ~ i ~ ` = Remove(indexes[i] - i, Args);`
```

where a compile time variable xi that is x0, x1, ... is created 
and this:

```d
`alias x` ~ i ~ ` = Remove(indexes[i] - i, x`~ (i - 1) ~ `);`
```

a similar idea but on the other side we have x(i-1) so for i = 1:

```d
alias x1 = Remove(indices[i] - i, x0);
```

Here is the template mixin approach for the first:

```d
mixin template MakeString1(alias i)
{
   mixin(`alias x` ~ i ~ ` = Remove(indices[i] - i, Args);`);
}
```

and the string mixin for the second approach:

```d
template Remove(long[] indices, Args...)
{
   enum N = indices.length;
   static foreach(i; 0..N)
   {
     static if(i == 0)
     {
       //template mixin approach
       mixin MakeString1!(i);
     }else{
       // string mixin apprach
       enum _string_ = `alias x` ~ i ~ ` = Remove(indices[i] - i, 
x`~ (i - 1) ~ `);`;
       mixin(_string_);
     }
   }
   mixin(`alias Remove = x` ~ i ~ `;`);
}
```

The `Remove` in the template body is an overload and works fine, 
but my attempt to use these approaches to dynamically overload 
has failed. I get errors like

```d
Error: no identifier for declarator x
```

Indicating that the concatenation `~` is not working, and I only 
get the partial string when I print out `pragma(msg, _string_);`


Thanks in advance for your help.



More information about the Digitalmars-d-learn mailing list