DIP 1027--String Interpolation--Final Review Discussion Thread

Adam D. Ruppe destructionator at gmail.com
Wed Feb 5 15:00:31 UTC 2020


On Wednesday, 5 February 2020 at 09:15:07 UTC, Johannes Loher 
wrote:
> instead but this is not what anybody who already knows about 
> string interpolation from any other language will expect. It is 
> really weird to have a string syntax (i"") where the result of 
> such an expression is not actually a string (or at the very 
> least implicitly convertible to one).
>
> Do we really want D to be the odd one out in this case?

Yes.

I understand this can be a pain and a bit surprising to new users 
- which is why my proposal special-cases the compiler error 
message to explain it - but in exchange for this inconvenience 
for that case, we gain a lot of benefits:

1) sql(i""); just works, with correct parameterization
2) jsx(i""); just works, with correct contextual encoding
3) writeln(i""); just works
4) printf(i""); can just work
5) Even readf(i""); just works!
6) Error detection is possible - at compile time - for all those 
scenarios
7) @nogc i"".toBuffer(...) is possible.
8) gettext() style internationalization is possible, including 
with the D compiler itself collecting the strings!
9) foo!i"" works to collect D aliases so like `my_debug!i"$a and 
$b"` might give
   a = 5 and b = 3
10) whatever else library authors can dream up!

All this while keeping it type-safe to detect improper or even 
just inefficient uses.

And, of course, `string s = i"".idup;` is still a very convenient 
option when you want it, but with so many functions just working, 
you may want to use them instead of string assignment!

Note that Javascript's template literals allow much of this stuff 
too and are really cool.


I'm chatting with a user on IRC now who feels this is a 
dealbreaker to him, it MUST implicitly convert. But the next best 
proposal for implicit conversion loses benefits #4 and #5 there 
(which I argued earlier in this thread was worth it!), makes #6 
still possible but less generally applicable, and makes #7 and #9 
pretty iffy. So I think `.idup` is an OK trade off here.

Like Steven, I encourage you all to look at i"" not being an 
interpolated string per se, but being a "string builder literal" 
or something like that - it is syntax sugar that gives an entity 
you can get a string out of rather than a string itself. An 
interpolated string is just one of the *many* things you can 
build with this new literal, and I expect that once you use it 
with some cool library support, you'll agree the explicit `idup` 
in some cases - and that's all you have to do to get the plain GC 
string out of it, no import necessary for that case in my 
proposal - is worth the gains we get in all these other cases too.


More information about the Digitalmars-d mailing list