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

Steven Schveighoffer schveiguy at gmail.com
Mon Dec 16 17:31:51 UTC 2019


On 12/14/19 8:21 PM, Walter Bright wrote:
> On 12/14/2019 6:47 AM, Steven Schveighoffer wrote:
>> What if the strings are all interpolated strings? e.g.:
>>
>> database.exec(i"UPDATE %table SET key=$key, value=${f}value "~
>>              i"WHERE index < ${d}index AND "~
>>              i"timestamp > ${D}lastTime");
> 
> 
> Then you'll get a tuple that looks like:
> 
>      "UPDATE %s SET key=%s, value=%f ", key, value, "WHERE index < %d 
> AND ", index, "timestamp > %D", lastTime

Why? I mean concatenation of Tuples isn't defined to do anything.

pragma(msg, AliasSeq!("hello", "there") ~ AliasSeq!("world", "and 
everyone else"));

Error: incompatible types for (tuple("hello", "there")) ~ 
(tuple("world", "and everyone else")): both operands are of type 
(string, string)

But we could define it for interpolated string tuples to be the 
concatenation of the format strings followed by the AliasSeq of all the 
fields in order.

This is the time to make these decisions, because it will be hard to add 
later. Removing automatic string concatenation was a great addition, but 
took a long time to get into the language, due to not breaking existing 
code. I'd hate to have to do it again.

>> I actually think 'i' should be able to go before any string literal.
>> e.g.:
>>
>> iq{$a + $b}
>> i`$a + $b`
> 
> Since the other method works, I am striving to keep things as simple as 
> possible. There needs to be very good reasons for gratuitous multiple 
> ways of doing something, and I don't see one here.

I'm proposing you DON'T make the other method work (it doesn't now). 
There doesn't need to be multiple ways to do string literals. And in 
fact, concatenation of interpolated string literals (as proposed above) 
would work well to provide the most expressive power, as you could 
concatenate any style of interpolated literals together, just like you 
can for uninterpolated literals.

And it's not complicated. The postfix characters of `c`, `w`, and `d` do 
not warrant complicated explanations in the spec, and are usable on any 
of the string literal types. An `i` prefix is similar.

All that is needed is to say "if you put `i` in front of a string 
literal (any string literal) it becomes an interpolated string literal"

> Of course, then 
> there's "what about qi strings", aaggghhhh.

That's not grammatically correct. It would be rejected. The 
"interpolated" property is orthogonal to the string literal method, and 
must come before it.

-Steve


More information about the Digitalmars-d mailing list