The state of string interpolation

Paul Backus snarwin at gmail.com
Mon Dec 10 06:14:28 UTC 2018


On Monday, 10 December 2018 at 05:43:48 UTC, H. S. Teoh wrote:
> On Sat, Dec 08, 2018 at 05:17:13PM +0000, o via Digitalmars-d 
> wrote:
>> 
>> As Paul Backus brought up in a PR, you can use 
>> `std.typecons.tuple`s instead, to use expressions. Example:
>> 
>> auto foo = tuple(i"a + b = $(a+b)");
>
> There is more than meets the eye here, that I think we should 
> be aware of, whether or not it affects the current prospective 
> DIP.
>
> [...]
>
> What we need for this DIP is for interpolated strings to be 
> lowered to the first kind of tuple, the compiler-internal 
> "tuple" that can capture AST-accessible "stuff" like string 
> literals and expressions.  I had thought it was equivalent to 
> AliasSeq, but apparently there's a discrepancy, and AliasSeq 
> cannot bind expressions which is a no-go. We cannot use 
> std.typecons.Tuple because it's generally frowned on for the 
> compiler to depend on Phobos (even though the ^^ operator 
> "cheats" in that respect -- but to be fair, that was a 
> historical accident predating the compiler/Phobos clean 
> separation policy). Furthermore, std.typecons.Tuple cannot be 
> used in static if and other AST stage constructs, so that's 
> also a no-go.
>
>
> T

To be clear: the proposal is, and has always been (to my 
knowledge), that interpolated strings should lower to a 
compile-time sequence, and not a Phobos Tuple.

The point I was making, in the PR "o" mentioned, is that you can 
pass the compile-time sequence you get from the interpolated 
string to `std.typecons.tuple` in order to store the values of 
the expressions in a Phobos Tuple at runtime--as though you had 
typed:

     auto foo = tuple("a + b = ", a + b);

In other words, it's an example of usage, not part of the core 
proposal.


More information about the Digitalmars-d mailing list