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

Steven Schveighoffer schveiguy at gmail.com
Wed Dec 11 22:49:01 UTC 2019


On 12/11/19 5:37 PM, H. S. Teoh wrote:
> Here's a potential 2-step fix:
> 
> 1) Change the interpretation of `%({X}varname)` to mean "use `X` instead
> of %s as placeholder in output string", rather than "use `%X` ...".
> I.e.:
> 
> 	i"%abc"		== tuple("%s", abc);
> 	i"%({%x}abc)"	== tuple("%x", abc);
> 	i"%({?}abc)"	== tuple("?", abc); // bingo, SQL syntax!
> 
> 2) Then, in light of the %%%% problem, and to fix the repeated % in
> "%{%x}abc", change the default metacharacter to something like $:
> 
> 	i"$abc"		== tuple("%s", abc);
> 	i"$({%d}abc)"	== tuple("%d", abc);
> 	i"$({?}abc)"	== tuple("?", abc);
> 
> For convenient interop with printf/writefln, we can still default to
> "%s" as the default placeholder, but the {} syntax now no longer assumes
> printf syntax, making i"" literals MUCH more useful in many more places
> outside the purvey of printf/writefln.
> 
> So you'd do SQL strings like this:
> 
> 	string name;
> 	int serial;
> 	float cost;
> 	db.exec(i"INSERT INTO mytable VALUES (${?}name, ${?}serial, ${?}cost)");
> 
> which translates the last line to:
> 
> 	db.exec("INSERT INTO mytable VALUES (?, ?, ?)", name, serial, cost);
> 
> without the need for any intermediate format string parser.

OK, this is definitely a winner, way better than my idea. Only thing 
better would be some way to set the default specifier to avoid all the 
verbosity.

Please make this part of the DIP Walter!

-Steve


More information about the Digitalmars-d mailing list