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

Q. Schroll qs.il.paperinik at gmail.com
Wed Feb 3 19:13:39 UTC 2021


On Wednesday, 3 February 2021 at 18:17:03 UTC, Adam D. Ruppe 
wrote:
> On Wednesday, 3 February 2021 at 17:47:19 UTC, Q. Schroll wrote:
>> Akin to `staticArray`, Phobos can (should) supply a template 
>> `asInterp` that forces a i"..." literal to be statically typed 
>> as a tuple around the respective `interp` sequence.
>
> Something to keep in mind is that tuples cannot actually be 
> wrapped in D. As soon as they are anything other than slices of 
> themselves or directly used as function parameters, they start 
> losing information.
>
>>     auto asInterp(string str, Args...)(interp!str first, Args 
>> rest)
>>     {
>>         import std.typecons : tuple;
>>         return tuple(first, rest);
>>     }
>
> Like here, if there was any refness in rest, it is gone in the 
> return value. If it was used as a template argument and had 
> alias parameters, that information is lost.

I never thought of that `asInterp` as the summit of creation but 
rather as a proof of concept. I thought about it for like 3 
minutes. The much better solution would be something akin to

     alias asInterp(alias interp!str first, string str, rest...) = 
AliasSeq!(first, rest);

used as

     asInterp!i".."

but unfortunately, this does not compile. (This was my first 
idea.) D cannot infer `str` from the value passed to it. It can 
for `interp!str` a run-time parameter type, but not for a 
template parameter. Since aliases are mere names, `ref`ness and 
stuff would be preserved (I guess so, since otherwise, forward 
wouldn't work).
Another, actually very simple, idea would be to give an i"..." a 
property asInterp that (cf. tuple's expand) returns the interp 
sequence.

I would have spelled out my critique differently had I read all 
feedback before; tuple was already mentioned. But the main point 
is: variadic templates are too common to assume they're all ready 
to take an interp sequence. As a very simple example different 
from tuple, consider emplacement functions that forward their 
arguments to constructors; all of them are variadic templates 
taking on almost anything. String arguments aren't too uncommon 
and wherever strings are used, we must assume that sooner or 
later someone uses an interpolated string. All of those variadic 
function templates would have to anticipate interpolated strings. 
When one thinks about handling strings, interpolated ones aren't 
too far fetched, but when one thinks about arbitrary types, 
interpolated strings aren't really the first or second thought. 
Variadic templates are the prime example of thinking of arbitrary 
types.

The @nogc people will hate it, but immediately making i"..." a 
string through allocation -- except "very specific circumstances" 
that leave absolutely no room for doubt -- is the only way this 
will interact with other language features in an intuitive 
manner. Having to use idup explicitly in any case is bad design. 
It makes interpolated strings inaccessible. They'd become a tool 
of the professionals and gurus, although being first and foremost 
designed to be an accessible productivity feature.
If code can be @nogc, programmers should have means to do that -- 
but that's it. It need not be stupidly easy at the expense of 
making an accessible productivity feature worse than it need be.


More information about the Digitalmars-d mailing list