Hacker News new | ask | show | jobs
by jacobn 1203 days ago
I'd also really have preferred $, but apparently there's some backwards compatibility issue with using that character?
1 comments

“${val}” is a legal string literal that is certainly used in plenty of existing programs.

In current Java you would have to escape \, so only “\\{val}” could have existed before.

That's why JS use ` and not reuse ".

You want the lexer to be able to make the difference between a constant string and a template string and you want users to be able to read the code.

This JEP solves the former not the later.

It’s not as bad as people make it out to be. Swift without any backwards compatibility constraints chose “\(val)”. It does need a slight getting used to but it is not any worse than ${}.
Swift’s choice is strange and ugly IMO, even if it did not involve backwards compatibility constraints.
For another n=1 opinion, I like Swift’s choice, even though it’s different from convention. It’s lighter, typographically. ‘${‘ draws more attention to the delimiters than ‘\(‘.

And yes, the delimiters could be made less obtrusive using syntax coloring, but not all tools will do that (e.g. when using grep on a code base)

Swift’s choice has the advantage of visually unifying the two cases where text in a string literal is interpreted as something other than the text itself. One is backslash escapes like \n and \\. The other is string interpolation.
Isn’t the STR.” prefix enough to distinguish traditional strings from format strings?
` would be even more difficult to type, and is easily confused with '.

On European (other than British/Irish) keyboards where it exists, it will often be a dead key.