DIP 1027---String Interpolation---Community Review Round 1
Steven Schveighoffer
schveiguy at gmail.com
Tue Dec 17 17:12:52 UTC 2019
On 12/17/19 5:14 AM, Patrick Schluter wrote:
> Yes, and that was the point of the exercice. Transforming the
> interpolated "string" into a string evaluates the values of the
> variables at the moment of that transformation. From then on, there is
> no interpolation possible as it a simple string.
> I suppose that what aliak leant in his comments was that the string then
> could still be used for interpolation.
>
> hypothetic scenario of what I interpreted what aliak wanted (and it
> won't compile, it's just for illustration)
>
> int apple;
> ....
> string my_is = i"apple=${apple}";
>
> ....
>
> apple = 36;
> writeln(my_is);
>
> would print "apple=36"
>
> with this DIP, when you do this
>
> int apple;
> ....
> string my_is = i"apple=${apple}".format;
>
> ....
>
> apple = 36;
> writeln(my_is);
>
> will print "apple=0" because my_is will contain the string "apple=0"
>
> The basic thing here is what Walter said above:
>
> interpolated strings are not string literals, they can't be. They are code.
Wow, I don't think that the first item can be done with the DIP. But
that would be really cool (I'm thinking of periodic logging features).
I tried this, still prints 5 apples and 6 bananas:
int apples = 5;
int bananas = 6;
auto as = AliasSeq!("%s apples and %s bananas", apples, bananas);
// essentially what an interpolated string will evaluate to
writefln(as);
++apples;
++bananas;
writefln(as);
It seems that AliasSeq doesn't store the alias (ironically), but the
value of the expression at the time.
Is there a way that this could work? I mean, aside from storing lambdas
for everything?
-Steve
More information about the Digitalmars-d
mailing list