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

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Dec 11 19:33:14 UTC 2019

On Wed, Dec 11, 2019 at 09:52:21AM +0000, Mike Parker via Digitalmars-d wrote:
> This is the feedback thread for the first round of Community Review
> for DIP 1027, "String Interpolation":
> https://github.com/dlang/DIPs/blob/148001a963f5d6e090bb6beef5caf9854372d0bc/DIPs/DIP1027.md

1) The way this DIP is worded belies its true power, which I suspect
some people are unaware of.  Part of this is because the writefln/printf
example chosen for the Description section makes it appear as though
this DIP is only about writefln or printf formatting.

It would be nice(r) if the DIP included a section describing use cases
outside of writefln.  For example: readable yet type-safe database query
formatting: by writing a format string parser that turns '%x' into '?'
and generates SQL bind commands for the corresponding arguments:

	string table = "mytable";
	string key = "key-that!needs'escap1ng";
	double value = 3.14159;
	int index = 12345;
	DateTime lastTime = getCurDate();

	database.exec(i"UPDATE %table SET key=%key, value=%{f}value WHERE index < %{d}index AND timestamp > %{D}lastTime");

(Note that '%D' is not a writefln-supported format, presumably it's
something database.exec() understands.)

2) The previous example does bring up another issue: is there a nice way
to handle long interpolated strings, i.e., wrap long interpolated
strings to multiple lines?  I.e., does the following work?

	database.exec(i"UPDATE %table SET key=%key, value=%{f}value "~
			"WHERE index < %{d}index AND "~
			"timestamp > %{D}lastTime");

If not, is there an alternative?  One of the key arguments of the DIP is
that traditional format strings become unwieldy when the length
increases and the number of arguments increases.  But how does the
proposed interpolated string syntax mitigate the very same problems in
its own syntax?

3) Someone has already pointed out the problem of interpreting %% as %,

	double grade = 89.5;
	writefln(i"Your grade is %{.2f}grade%%.");

gets turned into:

	double grade = 89.5;
	writefln("Your grade is %.2f%.", grade);

Note the dangling % at the end, which will writefln to throw an error.
Under the current syntax, you'd have to write "%%%%" to get a single '%'
in the writefln output, which seems excessive.

Counterproposal: %% should be copied literally as %% in the output.


PNP = Plug 'N' Pray

More information about the Digitalmars-d mailing list