Discussion Thread: DIP 1036--String Interpolation Tuple Literals--Community Review Round 2

Paul Backus snarwin at gmail.com
Fri Jan 29 13:21:18 UTC 2021


On Friday, 29 January 2021 at 13:03:45 UTC, Dukc wrote:
> On Friday, 29 January 2021 at 12:41:35 UTC, Paul Backus wrote:
>> You cannot actually pass an i"..." literal directly to `mixin` 
>> with this proposal; you have to call `idup` (or some other 
>> function) to convert it to a string first.
>
> Why? I understood it like this: 
> `mixin(i"foreach(i;0..${iterations}) foo();")` is first 
> attempted as `mixin(Interp!"foreach(i;0.."(), iterations, 
> Interp!") foo()"());`. Since `mixin` accepts only strings, 
> `idup` is implicitly called on the arguments.
>
> What went wrong?

mixin does not only accept strings:

     int n = mixin(1);
     assert(n == 1);

 From the language spec [1]:

> Each AssignExpression in the ArgumentList is evaluated at 
> compile
> time, and the result must be representable as a string.

In concrete terms, what happens is that mixin implicitly calls 
`.stringof` on each of its arguments. So your mixin example would 
be lowered to

     mixin(interp!"foreach(i;0.."().stringof, iterations.stringof, 
interp!") foo();"().stringof);

And evaluating the `.stringof` calls would result in

     // assuming iterations == 5
     mixin(`interp()`, `5`, `interp()`);

...which in this specific case happens to be a compile error, so 
it actually *would* be rewritten by the compiler to use `idup`. 
But there is no guarantee that will happen in general.

[1] https://dlang.org/spec/expression.html#mixin_expressions


More information about the Digitalmars-d mailing list