Feedback Thread: DIP 1036--String Interpolation Tuple Literals--Community Review Round 2

Steven Schveighoffer schveiguy at gmail.com
Thu Jan 28 14:58:36 UTC 2021


On 1/28/21 3:35 AM, Dukc wrote:
> On Wednesday, 27 January 2021 at 10:33:53 UTC, Mike Parker wrote:
>> [snip]
> 
> The DIP states that foo(i"a:${a}, ${b}.") is rewritten as 
> `foo(Interp!"a:", a, Interp!", ", b, Interp!".")`. It think it's better 
> to rewrite it as `foo(Interp!"a:", Interp!typeof(a)(a), Interp!", ", 
> Interp!typeof(b)(b), Interp!".")`. That way, `foo` has easier time 
> introspecting which came from the interpolated string.

First, I don't think it's critical for overloading, and will simply add 
to the template bloat. What are you going to do differently with `a` 
than you would with `Interp!(typeof(a))(a)`?

Second, this removes any ref possibilities for the parameters.

The parameters are guaranteed to start and end with an 
InterpolationLiteral, so one can assume that non-literal arguments are 
interspersed inside the literal.

> The type of interpolated string literal is very special cased. The DIP 
> states it is not an alias sequence, but that it behaves like one when 
> passed to a function. And if that does not compile, it is treated as 
> string instead. This is going to be full of all sorts of corner cases.

I was fully aware that this would be the most controversial part. I feel 
like it will not be full of corner cases, but I'm not sure. Can you 
specify any?

Consider a normal string literal can be used as a string, 
immutable(char)*, wstring, or dstring. I find it very similar to this 
feature, and I don't feel like there are a lot of corner cases there.

> Let me suggest an alternative: the user manually chooses the type. For 
> example, `i"hello ${world}"` would be rewritten as `idup(Interp!"hello 
> ", Interp!typeof(world)(world))`, and `I"hello ${world}"` would be 
> `AliasSeq!(Interp!"hello ", Interp!typeof(world)(world))`. And with 
> latter I mean an honest alias sequence, not one with a special cased 
> `.length` or anything like that.
> 

We have considered that. The problem is that people will use the string 
interpolation form without realizing the dangers or resulting bloat.

For instance, writeln(i"Hello, ${name}"), if made to proactively 
generate a string just to send it to writeln is extremely wasteful when 
writeln(I"Hello, ${name}") is not. I feel like the auto rewrite is a 
better option because it does the right thing in all cases. The beauty 
of it is that the library author gets to decide whether it makes sense 
to accept the expanded form, the user is just saying "here's something 
string-like I want you to handle". It puts the decision in the right 
hands, while not being intrusive in case the library author doesn't want 
to deal with it.

Consider also that code which uses a dual-literal system might have to 
use the string interpolation form because the library only allows that. 
Then at some point in the future, the library adds support for the 
expanded form. Now the user would have to go back and switch all usage 
to that new form, whereas an auto-rewrite would just work without changes.

-Steve


More information about the Digitalmars-d mailing list