"string interpolation"

Jonathan Marler johnnymarler at gmail.com
Sun Jun 9 07:05:26 UTC 2019


On Sunday, 9 June 2019 at 05:24:29 UTC, Amex wrote:
> On Saturday, 8 June 2019 at 22:23:34 UTC, Nick Sabalausky 
> (Abscissa) wrote:
>> On 6/8/19 5:28 PM, Amex wrote:
>>> I and many others write a code that uses string mixins simply 
>>> to define a symbol based on others:
>>> 
>>> mixin(T~" "~id~" = "~value~");");
>>> 
>>> 
>>> This is very ugly and it is impossible to debug properly.
>>> 
>>> Why not allivate this issue? Is is so common that there can 
>>> be a short hand syntax that the compiler can decode naturally.
>>> 
>>
>> There is work being done on the idea of interpolated strings:
>>
>> https://github.com/dlang/dmd/pull/7988
>
> This is not what I'm talking about. Similar but different. I'm 
> specifically talking about usage to replace string mixins.
>
> I'm not really talking about string interpolation at all. It's 
> symbol substitution.
>
> I've already given examples.

So it sounds like you want `#X` to lower to `mixin(X)`? So in 
your example

#T #id = #value;

would be

mixin(T) mixin(id) = mixin(value);

I think that using the `#` character as a stand-in for mixin has 
been proposed before, and I think the idea has merit.  However, 
as the language exists today there's no way to support the level 
of mixin you are suggesting.  Mixin only works at higher levels 
like expressions and statements, so you could do things like:

mixin("const a = 1 + 2 + 3");

and even things like:

const a = 1 + mixin("2 + 3");

but not things like this:

mixin("const a") = 1 + 2 + 3;

Or things like

const mixin("a") = 1 + 2 + 3;

Mixin can only appear at certain places in the grammar because 
the compiler needs to be able to analyze the code surrounding it 
before evaluating what's inside the mixin. This way you can use 
your own code at compile-time to generate what's inside the mixin 
without depending on what the mixin does to your code.

In any case, with Adam Ruppe's full string interpolation 
proposal, you're example can be done with:

mixin("$T $id = $value;");



More information about the Digitalmars-d mailing list