[Issue 24490] New: Disallow unescaped literal $ in a Interpolation Expression Sequence
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Apr 9 10:27:27 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24490
Issue ID: 24490
Summary: Disallow unescaped literal $ in a Interpolation
Expression Sequence
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: qs.il.paperinik at gmail.com
On [Interpolation Expression Sequence](https://dlang.org/spec/istring.html) It
says:
“A $ followed by any character other than a left parenthesis is treated as a
literal $ in the expression, there is no need to escape it.”
That is a bad idea. Just make $ without a following parenthesis an error. A D
programmer can easily escape it. Also, the fact that it has to be escaped makes
sense.
There are two good reasons:
1. If the string literal is changed later so that the unescaped $ happens to be
followed by an opening parenthesis, behavior changes.
2. It closes the door to introduce other uses for $ in IES literals forever
(or: without breakage).
For example, variable shorthands: In a lot of scripting languages, "This is my
$i-th attempt" puts the value of the variable `i` into the string. The same
literal in D (prefixed with i as `i""`) is legal and changing its meaning
further into the future is a bad idea. It rather have no meaning at all and be
i"This is my \$i-th attempt", or i"This is my $(i)-th attempt", which is what
the user probably wanted anyway.
Introducing variable shorthands later won’t make i"This is my $(i)-th attempt"
invalid.
I’d also suggest to require $ to be escaped in interpolated token sequence
strings. A backslash isn’t a valid token anyways, so in an `iq` string, \$
would be a special token that is translated to literal `$`, while unescaped $
would (for the time being) only be valid if a parenthesis follows.
About interpolated WYSIWYG strings, I’d go the same route as C# with `{{`
meaning a single `{` in an `@` string: Even here, `$` should be escaped,
however, because the main point of WYSIWYGs is that backslashes need no
escaping, my suggestion would be to require that a `$` is followed by a
parenthesis or another `$`, and double `$` means a single `$` in the string.
--
More information about the Digitalmars-d-bugs
mailing list