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

Steven Schveighoffer schveiguy at gmail.com
Fri Jan 29 13:57:49 UTC 2021


On 1/28/21 3:58 PM, Kagamin wrote:
> auto convoluted = i"${ir"`${"{"}`"}"; // nested string interpolations work.
> assert(convoluted == "`{`");
> 
> +InterpolatedString:
> +    InterpolatedDoubleQuotedString
> +    InterpolatedWysiwygString
> +    InterpolatedAlternateWysiwygString
> +    InterpolatedTokenString
> 
> Interpolated string should obey all escaping rules of the string literal 
> it's derived from, and initial lexing of such string should be done with 
> the same logic, and handling of interpolation sequences should be done 
> on raw content of the lexed string after all due unescaping.
> 
> i`\${.}`
> i"\\${.}"
> These two should have the same meaning of escaped interpolation dollar 
> sign, the escaped backslash becomes just backslash after double quote 
> string unescaping, and this backslash is interpreted as interpolation 
> escape sequence.

DoubleQuotedString is the only string with EscapeSequence processing. 
Therefore we continued that same expectation. The Wysiwyg string types 
specifically allow single backslash to represent a backslash, and we did 
not want to change that behavior.

In TokenString, the sequence ${ tokens } are not valid D tokens, so 
escaping the sequence isn't fruitful. It may be something that is 
reasonable inside a string literal inside the token string, but I don't 
think that's worth the complexity. If you want escapes, use the double 
quoted form.

Note also, to wait until the entire string is lexed to process the 
interpolation sequences means the sequences would have to obey the rules 
of the string. This means something like:

i"hello ${firstname ~ " " ~ lastname}"

if processed as a raw string first, would look to the lexer like 2 
string literals, one that is i"hello ${firstname ~ ", and one that is " 
~ lastname}". You would have to escape the quotation marks, e.g.:

i"hello ${firstname ~ \" \" ~ lastname}"

making the example more convoluted than just passing the parameters 
directly. In some string types, this wouldn't be possible (i.e. inside a 
wysiwyg string, you could not use the end quote type (` or ") in your 
expression).

The lexer already has the capability of processing token strings, which 
is essentially what this is. It's just in the middle of another string 
sequence.

-Steve


More information about the Digitalmars-d mailing list