DIP 1027---String Interpolation---Community Review Round 1

Adam D. Ruppe destructionator at gmail.com
Sun Dec 15 01:52:20 UTC 2019


On Sunday, 15 December 2019 at 01:44:18 UTC, Walter Bright wrote:
> The extra syntax kills it

This is true, but ONLY for the printf case. It just works in all 
other cases.

> with the extra import required, along with not working with 
> #betterC.

No need for import - this is a definition in object.d, and it 
will work in betterC since this is a compile-time transformation 
(the `asFormatString` definition is essentially just `template 
asFormatString(T...) { alias asFormatString = T; }`.

And my proposal also doesn't need an import to be assigned to a 
string whereas yours does.

string a = i""; // mine would trigger the alias toString this and 
just work

string a = format(i""); // must import std.format. also fails in 
betterC btw

> It's just not the default.

Which is usable (that's why I'm not opposed to your proposal) but 
means it is now impossible to catch accidental misuses at compile 
time. They type system no longer helps you.


But what about a small compromise: what if JUST the format string 
was given a new type and the rest was still a tuple. The format 
string can implicitly convert to the same things a string literal 
can; it can go to `string` or to `const char*`, but then 
user-defined functions are also able to overload on it.

So

i"hi %test"

becomes basically

typedef string InterpolatedFormat;

type_tuple(InterpolatedFormat("hi %s"),  test)


That'd still work in printf - it just implicitly converts back to 
char* - and gives just enough type information hints to cover my 
advanced use cases too.


More information about the Digitalmars-d mailing list