syntax for string interpolation
See original GitHub issueWe have received many complaints about the syntax for string interpolation in Ceylon. The double-backtick syntax was chosen because:
- double backticks are incredibly uncommon in regular text,
- we thought it looked quite visually pleasing, and
- it was very easy to lex, and therefore I figured it would be something that would cause less problems in the IDE.
In the end, the last item has not, to my mind, worked out anywhere near as well as I expected, and I think my reasoning on that was flawed.
Today I finally broke down, swallowed my pride and tried my hand at implementing something else. By wrapping the ANTLR token stream, and recursively lexing string tokens, I’ve been able to add support for the following syntax:
print("Hello, \(name)!");
Now, you were probably expecting this to be the more-common ${name}
instead of the less-common \(name)
. So why did I go for something slightly less familiar?
Well, \
is already the escape character in strings, and $
is not. So this is backward compatible. Also, \{...}
is already a syntax meaning a unicode escape sequence. So this is unambiguous.
Now, using the exact same technique, I could implement support for either ${ ... }
or \{ ... }
though the first would not be backward compatible, and the second would be a bit of a fiddle because the syntax would mean different things depending upon what occurs within the braces.
On the other hand, I think \( ... )
looks good.
I will push this to a branch, and I would like to hear some feedback.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:70 (56 by maintainers)
Top GitHub Comments
To be honest I really prefer the
\()
syntax. Not only does it use the already well-known\
character for escaping, but it also uses the familiar()
for grouping expressions.${}
feels completely alien to the language; I don’t have a strong preference towards either\()
or``
when compared to each other, but I do prefer either over${}
.If this is the time to break compatibility, then we should go with the more popular
${foo}
.