DIP 1027---String Interpolation---Community Review Round 1
    H. S. Teoh 
    hsteoh at quickfur.ath.cx
       
    Wed Dec 11 22:37:33 UTC 2019
    
    
  
On Wed, Dec 11, 2019 at 03:38:49PM -0500, Steven Schveighoffer via Digitalmars-d wrote:
[...]
> But there are a couple problems.
> 
> 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.
[...]
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.
T
-- 
VI = Visual Irritation
    
    
More information about the Digitalmars-d
mailing list