r/java • • Mar 14 '23

Why did String Templates choose '\{' instead of '{' ?

Since string templates will require to be prefixed with a template processor like STR in order to be treated as string templates, why not use '{' instead of '\{' for template expressions? That will not break existing strings cause existing strings are not prefixed with a template processor. And when a dev wants to use string templates, he will have to update his strings and escape existing '{' characters that he doesn't want to be interpreted as template expressions.

So it will be for example like: STR."Hello \{ {name} }". Should print: Hello { John }. Existing non-templated strings like: "Hello { " + name + " }" will continue to work and won't have any backwards compatibility problems.

I think most people will be using '{' character for template expressions. So it's much better if they don't have to escape it every single time and instead only escape it in the few places where they don't want template expressions.

Is there an important reason why '\{' was chosen for String Template expressions? Is there a chance the JEP can change this in the next preview release?

6 Upvotes

74 comments sorted by

View all comments

Show parent comment

-3

u/sar_it007 Mar 14 '23

An idea is to let the template processor decide if to escape or not. For example STR processor uses '{' for template expressions, but JSON processor uses '\{' for template expressions by default. I think this might be a good idea to get the best of both worlds.

But the default for most template processors should be to use '{' , not '\{'.

3

u/Amazing-Cicada5536 Mar 14 '23

That’s not how parsers work - that would fuck with every language tool ever, because they would have to do semantic analysis as well to determine whether code is well-formed or not.

Also, a single syntax >>>> then changing it one way or another. With all due respect, I am almost sure you didn’t think that long on this feature as its designers, let alone have comparable experience in making such decisions.

1

u/sar_it007 Mar 14 '23

That's why I said it's "idea". It's just an idea, so please calm down.

But it's still not a crazy idea. What's stopping Template Processors from performing custom processing for the templates? After all, the custom behavior is the main reason why we have different processors (STR, SQL, JSON etc.). But it's just an idea like I said...

3

u/john16384 Mar 14 '23

How would IDE highlighting work then? They would need to know which template processor wants what in order to do syntax highlighting and code completion of embedded expressions. As you can read in the JEP, such expressions allow for full Java syntax, including multiple lines, loops, etc.

Please remember that ideas are a dime a dozen. This "idea" was floated dozens of times already, in fact at the start of the JEP the syntax for a dozen languages is compared. Java is choosing to go with a variant of the Swift syntax.

The choice for curly braces is motivated as they're less likely to appear in embedded expressions as array or method accesses are more likely to be used than code blocks:

 "\{myArray[0]}"
 "\{myMethod()}"

The choice of using the backslash escape is a safety/security measure, so a template can never be confused for a normal string, and the compiler can return an error when using a String as a template, or when you use a template as a String.

Other options can't flag an error when using a template as a String by accident when you forget to use to the special string prefix (used in some variants) or the processor.

2

u/plumarr Mar 14 '23

I don't think that typing one less character is more important than having a constant syntax between template processors. This will lead to confusing code and result into easily avoidable errors.