String mixin from within a template

data pulverizer data.pulverizer at gmail.com
Sun Aug 23 20:54:22 UTC 2020


On Sunday, 23 August 2020 at 20:12:12 UTC, ag0aep6g wrote:
> On Sunday, 23 August 2020 at 19:42:47 UTC, data pulverizer 
> wrote:
>> `alias x` ~ i ~ ` = Remove(indexes[i] - i, x`~ (i - 1) ~ `);`
> [...]
>> ```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_);`
>
> You're having a misconception about concatenation.
>
> This:
>
>     "foo " ~ 65 ~ " bar"
>
> does not produce the string "foo 65 bar". It produces the 
> string "foo A bar", because 65 == 'A'. In your case, when i == 
> 0, you get a null byte in the string, which is probably the 
> cause for the partial output.
>
> You can use std.conv or std.format to make a string from a 
> number:
>
>     text("foo ", 65, " bar")
>     format("foo %s bar", 65)

Thanks for your update. I have changed the code:

```d
import std.conv: text;
template Remove(alias indices, Args...)
{
   enum N = indices.length;
   static foreach(i; 0..N)
   {
     static if(i == 0)
     {
       enum _string0_ = text(`alias x`, i, ` = Remove(indicies[i] 
- i, Args);`);
       pragma(msg, "compiled string 1: ", _string0_);
       mixin(_string0_);
     }else{
       enum _string1_ = text(`alias x`, i, ` = Remove(indicies[i] 
- i, x`, (i - 1), `);`);
       pragma(msg, "compiled string 2: ", _string1_);
       mixin(_string1_);
     }
   }
   enum _string2_ = text(`alias Remove = x`, N - 1, `;`);
   pragma(msg, "compiled string 3: ", _string2_);
   mixin(_string2_);
}
```

and I am getting the following errors:

```
compiled string 1: alias x0 = Remove(indicies[i] - i, Args);
Error: found - when expecting )
Error: semicolon expected to close alias declaration
Error: no identifier for declarator i
Error: declaration expected, not ,
... repeated for the other two cases ...
```

Thanks



More information about the Digitalmars-d-learn mailing list