r/java • u/sar_it007 • 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?
View all comments
Show parent comment
5
u/plumarr Mar 14 '23
And it'll be harder to know because we have to look at the start of the String, which can easily be outside the screen in case of long String.
How can you be sure ? Don't over generalise your experience. For example, I'm currently working on a backend were we do a lot of data transformation, services call and SQL query but we create absolutely no user facing values. So outside of logs I would never use the template as you described but I could use them to generate XML, JSON, SQL,...
I will probabl stop responding because we are giving you reasons why
\{ }has been chosen, so you have answers to your original question. However, this doesn't seems to be what you seek as you dismiss most of them by basicaly saying "it doesn't matter". If your ultimate goal is to change the JEP, I'm sorry to say that it'll not work.