Discussion Thread: DIP 1036--String Interpolation Tuple Literals--Community Review Round 2
Q. Schroll
qs.il.paperinik at gmail.com
Thu Feb 4 17:39:50 UTC 2021
On Thursday, 4 February 2021 at 16:31:17 UTC, Arafel wrote:
> On 4/2/21 17:15, Meta wrote:
>> However, if you want a tuple sequence as described in the DIP,
>> you can simply call a (probably compiler-supplied) helper
>> method; maybe it doesn't even have to be new: why not .tupleof?
>>
>> auto apples = 2;
>> auto bananas = 3;
>>
>> auto s1 = i"I have ${apples + bananas} fruit";
>> static assert(is(typeof(s1) == string));
>>
>> auto s2 = i"I have ${apples + bananas} fruit".tupleof;
>>
>> Then we can hide all the complexity of interpolated sequences
>> behind the .tupleof (or whatever we decide on) magic property.
>
> Could this be made so it's transparent to the user of a library?
>
> I.e. in the SQL case, the user of a library just types:
>
> ```
> auto result = connection.execute(i"SELECT * FROM foo WHERE bar
> = ${baz}");
> ```
>
> instead of
>
> ```
> auto result = connection.execute(i"SELECT * FROM foo WHERE bar
> = ${baz}".tupleof);
> ```
>
> If it has to be explicit, how would the signature of
> Connection.execute(...) look like? How would the user easily
> know that the function expects an interpolated literal, just by
> looking at it?
When Adam D. Ruppe wrote about SQL injections, I thought about
that. The signature of `execute` must not take a string as the
only parameter for sure. This is the basic pattern for accepting
an interpolated sequence.
auto execute(string str, Args...)(interp!str first, Args
rest) { }
If you call `execute` with `execute(i"SELECT ${name} FROM
table"), the compiler tries i"SELECT ${name} FROM table".tupleof
and finds a PERFECT match to the above template. If that
`execute` is the only one available, you cannot use it with
something other than an interpolated string as the first
parameter; nothing else (like a regular string literal) will
match `interp!str`.
More information about the Digitalmars-d
mailing list