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

Walter Bright newshound2 at digitalmars.com
Sat Dec 14 08:51:54 UTC 2019


On 12/11/2019 12:38 PM, Steven Schveighoffer wrote:
> This is very much focused on writef and printf. What about other functions that 
> accept similar string + arg tuple, but don't use %s specifiers? Perfect example 
> is SQL:
> 
> query("select * from sometable where date < ?", someDate);
> 
> With the interpolation string:
> 
> query(i"select * from sometable where date < %someDate");
> 
> This translates to:
> 
> query("select * from sometable where date < %s", someDate);
> 
> which is not correct SQL syntax.

It's a great point. I propose a modification:

Change the `{FormatString}` from being a suffix to `%` to being the whole format 
string, i.e. instead of `%{d}` make it `%{%d}`. Then, your example becomes:

   query("select * from sometable where date < %{?}someDate");

which gets rewritten to:

   query("select * from sometable where date < ?", someDate);

and voila, any format specifier can be used. Robert brought up an additional 
problem:

   printf("%.*s", length, ptr);

as not being representable as an interpolated string. But this change will allow 
it to work:

   printf(i"%{%.*}length%{s}ptr");

The default format will remain `%s`, as I expect that will be the vast bulk of uses.


> The other problem is that you are using % for the interpolated fields. This is 
> quite puzzling, considering that the main target (printf and writef) uses % as 
> the format specifier. Why not just change the specifier to $ or {} or \() or \{} 
> or literally any of the other interpolation systems out there.

I've decided to change it to $. That looks nice. The \ ones just look awful, and 
  {} leaves open the problem of where to put the user-specified format.


> If we can think of a way to hook the string generation and have the parameters 
> come afterwards, that would be ideal.

Lowering it to a tuple is good enough. You can do whatever you wish with the 
resulting tuple (like pass it to a template).


More information about the Digitalmars-d mailing list