Discussion Thread: DIP 1036--String Interpolation Tuple Literals--Community Review Round 2

Arafel er.krali at gmail.com
Fri Jan 29 10:53:14 UTC 2021


Replying here to Walter's post in the Feedback Thread:

https://forum.dlang.org/post/rv0grf$g1j$1@digitalmars.com

> Let's compare mysql:
> 
> DIP1036:
> 
> mysql_query(i"select * from foo where id = ${obj.id} and val > ${minval}");
> 
> DIP1027:
> 
> mysql_query(i"select * from foo where id = ${?}(obj.id) and val > ${?}minval");
> 
> The DIP1027 could be shorter or longer, depending on if the ( ) were needed or not. DIP1036 also requires a user-written overload for mysql_query(), DIP1027 does not. DIP1036 is not a clear winner for mysql. 

Perhaps DIP1027 makes life easier for the library developer. As a 
_user_, however, DIP1036 wins by a landslide, because the placeholder is 
an internal implementation detail that needn't and shouldn't be exposed.

In fact, different SQL "dialects" can use different placeholders, so 
DIP1036 allows the following abstraction:

```
auto preparedStatement = connection.prepare(i"select * from foo where id 
= ${obj.id} and val > ${minval}")
```
This will then be translated by the actual class implementing the 
Connection interface to the proper version:

Postgres:

```
postgres_prepare("select * from foo where id = $1 and val > $2", obj.id, 
minval);
```

MySQL:
```
mysql_prepare("select * from foo where id = ? and val > ?", obj.id, minval);
```

Furthermore, the Postgres syntax becomes almost useless with DIP1027 if 
the user still has to keep track of the position of each argument, and 
for instance update all of them when adding one at the beginning.

As the user of a library, if I use an interpolated string, that's 
exactly the kind of thing I expect it to manage automatically for me.

Of course one could settle on a default placeholder and then reparse the 
string to adapt it as needed, that's in fact what JDBC does, but... here 
we have the chance to do the right thing from the beginning.

Finally, although a bit secondary, I personally find that the syntax in 
DIP1027 is somewhat awkward: If I see `${?}minval`, or even 
`${?}(obj.id)` I intuitively expect only `?` to be part of the 
interpolation, that's how it works in many languages (bash, PERL) when a 
variable is expressed as `${...}`, it specifically makes clear that 
everything out of the brackets is not included.


More information about the Digitalmars-d mailing list