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

Adam D. Ruppe destructionator at gmail.com
Sun Feb 2 23:54:35 UTC 2020


I'm dropping other stuff just cuz I don't want to argue down 
rabbit holes. Let's see what common ground we can find and build 
upon that.

On Sunday, 2 February 2020 at 20:13:36 UTC, Steven Schveighoffer 
wrote:
> I think we could get the best of both worlds if the 
> interpolated string itself was not just a string, but rather a 
> library-defined type (well something slightly more special -- 
> it should implicitly cast to a null-terminated immutable(char)* 
> if needed, just like string literals).

Yes, indeed, I proposed this in the last round too. I think it is 
our best bet for a compromise to salvage this DIP.

You would lose the methods on it ... however, we have UFCS, so 
not a deal breaker.

Lots of the community is opposed to the requirement to import 
something for the UFCS flavor; that is a psychological barrier 
for many people. I'm not in love with that myself, but I can live 
with it.

And I remain legit concerned about implicit conversions though:

printf(i"$some_int");

that looks OK, but SILENTLY DOES THE WRONG THING.  But if dmd 
warned on the format specifier like gcc does, we're cool. Could 
be an enhancement later. I warn about this but do not withhold 
support about it.


The DIP mentions this one: `printf("%s $something", foo);` This 
also does the wrong thing. But so does `printf(i"$something% 
complete");`, since `% c` is a valid format specifier.... yet it 
doesn't look like one, especially since the interpolated string 
use $ now.

We need to make % go ahead and get translated to %% by the 
compiler so the parsing function can still work sanely with it. 
That's the solution to this. If the compiler makes % magic in the 
generated string, it needs to encode % in the user input too.

Then put a type on the format string, if it implicitly converts 
is up to y'all.  I just need some way to detect the format 
string's presence via in the type system for overloading existing 
functions.

Do those two small changes and this DIP will have my support.

* specify that % in the user string gets translated to %% in the 
format literal
* put the format literal in a wrapper type.

i"$foo %"

is translated to the tuple:

__d_format_literal!("%s %%"), foo


struct __d_format_literal(string fmt) {
         enum f = fmt;
         alias f this;
}

That implicitly converts and just works in printf. That's the 
answer.

> And we can actually add later the idea of making the 
> interpolated string a new type after this DIP is implemented,

As soon as this DIP is implemented, we're frozen. Any future 
change will face additional barriers as a breaking change. It is 
going to be a lot better to just do it at least somewhat 
correctly now.


gah i've been trying to edit this post for like 90 mins now. im 
just gonna hit send; i 2g2 and i think you get the gist of what i 
mean.


More information about the Digitalmars-d mailing list