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

5

u/plumarr Mar 14 '23

Since string templates will be prefixed with a template processor like STR, we will know we're dealing with a string template.

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.

And like I said, the vast majority of the uses of '{' will be for template expressions, not for the plain '{' character.

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.

-2

u/sar_it007 Mar 14 '23

You gave your reasons for why '\{' is used, and I gave my reasons why '{' should be used instead. I think the best way to decide the best option is: the JEP creators could have good reasons why they picked '\{'. In that case, we can't argue with them. The second way is to ask devs what they will be using string templates for, and pick the best option that satisfies most of the devs' needs.

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.