"string interpolation"

Amex Amex at gmail.com
Mon Jun 10 08:53:56 UTC 2019


On Sunday, 9 June 2019 at 07:05:26 UTC, Jonathan Marler wrote:
> 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;");


There is nothing that would stop the language from doing what I'm 
suggesting...


You agree that every statement S could be rewritten to 
mixin("S"); and then evaluated by the compiler?

If so, then it can do what I suggest.

1. Scans statement for any symbol starting with special char(#,$, 
or whatever).

2. If the statement has such a symbol then the statement is 
wrapped in the mixin but all the symbols are expanded first.


.e.g,


$T $id = $value;

$ is found in front of a symbol.

simply expand symbols

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

with the appropriate to!string's when needed.

It is simply a rewrite rule that simplifies meta code and makes 
certain string mixins more readable by making them look like 
standard code. It makes it easier to debug. It is only for symbol 
substitution so there is always a 1 to 1 relation. Any errors in 
the resolved string can be mapped back in to the original 
statement.


It's purpose is to get rid of explicit mixin entirely for certain 
types of very common string mixin statements.



$T $id = $value;

can be lifted to mixin without any compiler knowledge. It is a 
direct map. The only issues may be in determining how to convert 
the symbols to strings... but in most cases to!string will work 
fine.





More information about the Digitalmars-d mailing list