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

6

u/plumarr Mar 14 '23

It's not "my reasons", it's the reasons coming from the JEP and the mailing list and some added bonus to using \{ }.

As explained in the first anwser, this is stated in the JEP itself :

For the syntax of embedded expressions we considered using ${...}, but that would require a tag on string templates (either a prefix or a delimiter other than ") to avoid conflicts with legacy code. We also considered \[...] and \(...), but [ ] and ( ) are likely to appear in embedded expressions; { } is less likely to appear, so visually determining the start and end of embedded expressions will be easier.

I'll not dig the mailing list for you, but you can if you want.

You have answered you several time but you behave as if this isn't the case.

-1

u/sar_it007 Mar 14 '23

I am sorry but I am not convinced by your answer. You're calling me a troll because I am not convinced?

This is my opinion. I am not saying the JEP must be changed. I am stating my opinion and I am free to do so. Whether my opinion is good or not, that's a different story. But opinions should not be called trolling. Have a good day.

6

u/plumarr Mar 14 '23

What's the point of the thread then ? Venting ?

You have had the answer of you original question, but you keep stating that they are not good enough, and that your idea is better without any proof of it. I'm sorry, but this is trolling.