5 ms·
> Thus if we forget to use a template processor such as STR, RAW, or FMT then a compile-time error is reported: String name = "Joan"; String info = "My
by scotty79 3y ago
> Thus if we forget to use a template processor such as STR, RAW, or FMT then a compile-time error is reported:
String name = "Joan";
String info = "My name is \{name}";
| error: processor missing from template expression
Were literals like "\{" always causing an error?
- ahtihn 3y agoYes, because it's the escape character. It's the reason they didn't chose "${" like many other languages. Because that's a valid sequence in a string literal.
- masklinn 3y agoWhich is a nonsensical reasoning they're also requiring a template processor, so string templates were previously impossible in two different ways. `\{` as an interpolation leader would make sense if untagged string literals became implicit STR templates, but that's not the case.
- scotty79 3y agoI guess they want to force you to choose interpolator. So that you won't implicitly use STR when you should have explicitly use SQL.
- masklinn 3y ago> I guess they want to force you to choose interpolator. In which case using `\{}` as the interpolation signal is completely unnecessary, because the new syntax already solves the backward compatibility concerns. Using just `{x}` like C# and Python, or `$x`/`${x}` like Kotlin, would have sufficed and made the syntax a lot more palatable. So would using postfix format specifiers, because that printf-style prefix garbage is just gnarly.
- scotty79 3y ago\{ protects you from forgetting to use interpolator at all. No other syntax that uses same delimiters as string literal would.
- deleted 3y ago[deleted]
- masklinn 3y agoYes, the escape sequence `\{` is not valid and javac would report an "illegal escape character" error.
- foobazgt 3y agoYes. '\' denotes an escape sequence, and "\{" was/is not a valid escape sequence.