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

Adam D. Ruppe destructionator at gmail.com
Sat Dec 14 14:23:56 UTC 2019


On Saturday, 14 December 2019 at 09:10:47 UTC, Walter Bright 
wrote:
> I find your proposal a little confusing. By turning the string 
> into an object, it doesn't work with printf?

Right, not directly. But then it is trivial to add a member of 
the object that transforms it into a tuple suitable for printf.

So the end user's code now looks like: `printf(i"foo 
%bar".asFormatTuple);`

BTW I am not opposed to your version either, since it can be 
transformed into something close to my version. My big concern is 
that your version (as well as Jonathan Marler's) loses 
information very easily.

printf(i"%item"); // happens to work
writefln(i"%item"); // also happens to work

writeln(i"%item"); // oops! not going to do what you want

format(); // works
text(); // wrong

And there's no compile error for incorrect usage. With an object, 
we get existing type checking safety and can guide it to the 
correct usage:

printf(i""); // error cannot implicitly convert 
__d_interpolated_string to const char*

writefln(i""); // static assert failed: "use i"".asFormatTuple 
instead

writeln(i""); // happens to just work because it calls toString

> I'm also not sure it's a good idea to make this more powerful. 
> As a simple "replace the string with a tuple" its very easy to 
> understand.

Objects are easy to understand too. And easy to implement: on the 
compiler it is basically just `X!(your version)` instead of just 
plain `(your version)`.


More information about the Digitalmars-d mailing list