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

Arine arine123445128843 at gmail.com
Wed Feb 5 16:02:03 UTC 2020


On Wednesday, 5 February 2020 at 15:00:31 UTC, Adam D. Ruppe 
wrote:
> 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.

A better feature would be if you could just create your own 
function then. Cause having to put %d/f/etc... when you want to 
use printf is just backwards. C++ has a feature that lets users 
process strings, and they can customize how it gets interpreted. 
We can do something similar.

> 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!

These don't "just work". You have to manually add the specifier 
based on what you are calling.

If you can define your own interpolated string implementation, 
then it can.

     float value;
     string table;

     // current proposal, doesn't "just work":
     sql(i"SELECT * FROM $table WHERE value = $value");
     sql("SELECT * FROM %s where value = %s", table, value); // 
error

     // now it works, but not "just":
     sql(i"SELECT * FROM ${?}table WHERE value = ${?}value");
     sql("SELECT * FROM ? WHERE value = ?", table, value);

     // just works:
     sql(sql_i"SELECT * FROM $table WHERE value = $value");
     sql("SELECT * FROM ? WHERE value = ?", table, value);

     // current proposal, doesn't "just work":
     printf(i"The $table and $value");
     printf("The %s and %s", table, value); // error + unsafe

     // now it works, but not "just":
     printf(i"The $table and ${%d}value");
     printf("The %s and %d", table, value); // ops, still "works"

     // ok now for real it works, but not "just"
     printf(i"The $table and ${%f}value");
     printf("The %s and %f", table, value);

     // just works:
     printf(fmt_i"The $table and $value");
     printf("The %s and %f", table, value);

Otherwise if you have to manually specify what specifiers to use, 
then this is dead on arrival.

-1 my vote for the proposal if you have to manual put specifiers, 
someone should put up a poll.






More information about the Digitalmars-d mailing list