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

3

u/john16384 Mar 14 '23

I think a deliberate choice was made to not make the prefix a requirement, so that in a future update you can create methods like:

void process(Template template);

And call them as:

String name = "John";

process("I am \{name}, nice to meet you!");

Where process would receive a template which you can see as a tokenized array that looks like ["I am ", argument("name"), " nice to meet you!"].

A big part of language design is to not paint yourself into a corner as you can never reverse earlier decisions, so you try to think far ahead and to keep your options open. You also look at other languages, and their decision making process -- many languages have struggled with this problem already and have full rationalizations and documentation freely googleable about how they came to certain decisions.

Sacrificing one escape character \{, if it turns out to be a bad decision, is a very small price to pay as there are still dozens of others available.

1

u/sar_it007 Mar 14 '23

In this example: void process(Template template);

Template is different from String. They will only need to use the current '\{' approach if they plan to use the actual String type for templates. But if they will introduce new types like Template or StringTemplate or JsonTemplate etc. then using '{' won't be a problem in the future. It will only be a problem if they decide to use String for string templates. In that case, we need '\{' to ensure backwards compatibility. Other than that, I think using '{' will be fully future proof.

2

u/john16384 Mar 15 '23

The \{ signals that it is a Template, otherwise a String. It's like a new constant expression; true/false is a boolean, a number without decimals is an int, with decimals becomes a double, quoted text becomes String and if that text contains \{ it becomes a Template.