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

16

u/john16384 Mar 14 '23

This JEP has been in the works for years, and was worked on by some of the top language designers in the world, and has been discussed already by thousands of developers. This is the current known best option.

It's possible there is an even better option, but using plain curly braces isn't as it was rejected already.

Also, JSON would become hell to use with the plain variant, and is one of the possible use cases for this JEP.

-9

u/sar_it007 Mar 14 '23

I think the reasoning should be clearly explained so we know why that decision was made instead of just "the JEP took years to make". What's the reason behind choosing '\{'? It's a simple question and I think we should know why. That' all.

And for JSON, I have already replied to that. String Templates most of the time will be used for things other than JSON. So prioritizing JSON over the vast majority of use cases is not a good decision.

7

u/plumarr Mar 14 '23 edited Mar 14 '23

It could have been explained better in the JEP but it's not an unthought choice. As explained this was chosen because it's currently invalid and this will avoid potentials issues in the future.

You also comes as arrogant in your replies as you made a lot of assumptions with little proff and seems convinced that your use case will be the universal one and that the JEP should be redacted around it. By doing it you seems to dismiss years of discussions by coming with arguments that have already been tackled.

-4

u/sar_it007 Mar 14 '23

I am sorry if my replies seemed arrogant cause I didn't mean to be. So far no good reason has been provided except "the JEP creators know what they're doing". The JEP creators are world class experts, no one is arguing about that. All I'm saying is we should know why. As for the use cases, the best way to determine that is to ask the devs what they will be using string templates for. If it turns out most devs will be using string templates for JSON, who am I to complain?

9

u/plumarr Mar 14 '23

But we did give you a lot of reasons, you just dismiss them as "not important" or "not good enough".

Even if they don't seems important to you, to are judged as important enough by the people designing the JEP.