DIP 1027---String Interpolation---Community Review Round 1
Patrick Schluter
Patrick.Schluter at bbox.fr
Mon Dec 16 12:55:17 UTC 2019
On Monday, 16 December 2019 at 12:37:47 UTC, Jacob Carlborg wrote:
> On Monday, 16 December 2019 at 11:18:33 UTC, Patrick Schluter
> wrote:
>
>> Maybe I'm wrong here, I haven't thought it through, but in
>> first approach I think that's an essential point. As the
>> string contains code, if evaluated at CT, the string must be
>> split somehow in literal parts and code parts. Allowing the
>> i-string to be handled as regular string implies that the
>> splitting happens sometime between when it is declared and
>> after it is used, which can happen after runtime.
>>
>> import fn;
>>
>> int b = 20;
>>
>> string inter = i"$(a+b)";
>>
>> foreach(a; 1..10)
>> fn(inter);
>>
>> ---
>> module fn;
>>
>> void fn(string s)
>> {
>> writefln(s);
>> }
>>
>> How would that work?
>>
>> In python or javascript there's no issue, a is a runtime
>> symbol.
>
> Not sure how other languages do it but in Ruby the
> interpolation is performed where the string literal is
> declared. Not where the string is later used. So the above
> would fail because `a` is not available.
>
Yes, probably. The issue I had with the transformation as string
is what should the code below print?
import fn;
int a, b = 20;
string inter = i"$(a+b)";
for(a=0; a<10; a++)
fn(inter);
---
module fn;
void fn(string s)
{
writef(s);
}
prints
202020202020202020
or
212223242526272829
and that's the difference between CT evaluation and RT evaluation.
More information about the Digitalmars-d
mailing list