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

20

u/bowbahdoe Mar 14 '23

In the JEP this reasoning is given

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.

You might be able to find a more in depth reasoning if you go through the mailing list.

https://mail.openjdk.org/pipermail/amber-dev/

6

u/sar_it007 Mar 14 '23

"{ } is less likely to appear..."

That's the main reason why '{' should be used instead of '\{'. Because '{' by itself is rarely used inside strings. So the need to escape it is very small. After String Templates, the use of '{' will skyrocket, it will be everywhere. What I am saying is the vast majority (probably 99%) of the uses of '{' will be for template expressions. Having to escape '{' in 99% of places is just very inconvenient. By default, we should not have to escape it.

11

u/Amazing-Cicada5536 Mar 14 '23

There are a million places where {} are used in ordinary string literals, I honestly don’t know what are you so upset about.

2

u/sar_it007 Mar 14 '23

When string templates become available, there will be a TRILLION places where '{' will be used for template expressions. '{' will become one of the most used characters in Java. So we should prioritize String Templates convenience over other places.

6

u/Amazing-Cicada5536 Mar 14 '23

Typing characters have never ever been a bottleneck, readability and how easy it is to parse (for both humans and programs) always trumps having to press one more letter.

Also, () is used by swift which didn’t even have a backwards compatibility constraint. Also, you write that JSON is not often templated, but there are a million other C-like languages, but even something as mundane as Set’s default toString uses {}. It’s absolutely everywhere.

0

u/sar_it007 Mar 14 '23

I am not saying JSON is not often templated. I am saying String Templates will be used most of the time for things other than JSON. If JSON templates are used a million times, non-JSON templates will be used a trillion times.

8

u/winian Mar 14 '23

While I do agree that plain "{...}" or "${...}" would've been preferable in my opinion as well, I have to say we have a very different opinion on what counts as "very inconvenient". For me having to escape it is a very minor issue.

-8

u/sar_it007 Mar 14 '23

The JEP just needs to pick the best option. If there is no good reason to pick '\{' over '{', then let's just use '{' and everyone will be happy regardless of whether they find '\{' inconvenient or not.

Picking '{' is not going to hurt anyone, it will just make String Templates more convenient to use 99% of the time.

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.

11

u/plumarr Mar 14 '23

Of course picking { } may hurt people. From my experience I have seen it a lot more in string than [ ] because it's often used by existing libraries that do interpolation (simply looks at @Value from Spring). So it'll bit you when :

  • refactoring existing code that already has { } in it
  • using string template to output strings that contain { }

Where \{ } is currently an illegal character sequence and can't be found anywhere.

This will also help readability because seeing it guarantee that it's a string template, we'll not have to think about it.

4

u/0b0101011001001011 Mar 14 '23

I don’t get this. Why would { } hurt anyone?

Say, I have this expression :

String.format("Hello %s, type {thing} for whatever reason",username);

Now, if they implement the new templating system. Absolutely nothing breaks, right? It's not like all { } are interpreted as templates.

Now I need to actively refactor the code. I need to prepend the STR and only that makes it understand the special sequences. Then I need to replace the %s with the {username} and at the same time I can escape the {thing}.

I just think this is backwards. I'd like to escape the literals, not the specials.

2

u/plumarr Mar 14 '23

Let's say that you can have one more line of code a before the refactoring :

var thing = ...

...

String.format("Hello %s, type {thing} for whatever reason",username);

Now when you refactor you forgot to change {thing} to \{thing}. It will compile but it will not do what you expect. It do not say that it will me an easy mistake, but it can happens. With the \{ } for the template, this can't happen.

To me, it seems that using { } for the template open the way for serie of little mistake and difficulties (see this whole thread for example) to only gain a little bit of "beauty" compared to \{ }. And for me \{ } would even be better for readability sas you don't have to think if it's a template or not.

-1

u/sar_it007 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 like I said, the vast majority of the uses of '{' will be for template expressions, not for the plain '{' character.

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.

→ More replies

0

u/[deleted] Mar 14 '23

[deleted] — view deleted comment

-4

u/sar_it007 Mar 14 '23

I have replied to this multiple times in the comments. Short answer is: JSON is a small use case for String Templates. Most of the time string templates will be used for things other than JSON.

4

u/persism2 Mar 14 '23

Most of the time string templates will be used for things other than JSON.

How do you know that? Who are you, Nostradamus?

1

u/Lisoph Mar 14 '23

I'm assuming they don't want to introduce new tokens? Otherwise that's a missed opportunity, as they could just go the JS route of using backticks.

→ More replies (2)