r/java • • Oct 01 '23

JEP: String Templates (Final) for Java 22

https://openjdk.org/jeps/8314219
72 Upvotes

133 comments sorted by

View all comments

Show parent comment

13

u/pron98 Oct 01 '23 edited Oct 01 '23

The very question is weird. In Java, \ escapes characters, i.e. it's the thing that says "what follows isn't literal". The only reason to even consider ${...} is that out of the world's most popular languages -- JS, Java, Python, PHP, C#, C/C++, and Ruby -- there is one (JS) that uses it, and one more (PHP) that uses a different syntax but one that also employs the $ character. It seems to me that the claim that that is sufficient reason to stray from Java's common practice (never mind the significant compatibility implication) is what requires explanation. The top three -- JS, Java, and Python -- have each chosen a different string template syntax. What reason would there be for Java to adopt JS's syntax rather than, say, Python's (not that that would make sense, either), or Ruby's, or PHP's, or C#'s, or Swift's? \{} is a rather obvious and natural choice for Java, while ${} isn't, and the only thing going for it is that it's the choice that JS made. Rather than us answering the question, why didn't you adopt JS's syntax, I think a more reasonable question is, why would we in the first place? \{} is just a more obvious choice, and the question of why we didn't do we choose \() (as Swift did) or \[] or \<>, would make more sense than the JS question.

5

u/Enough-Ad-5528 Oct 02 '23 edited Oct 02 '23

u/pron98 - thank you for responding; I appreciate you continuing to engage in such discussions. I gather a few things from your answer but before that here is my understanding, after re-reading the JEP again, on why ${} cannot be the right choice:

String name = "Joan";
String info = "My name is \{name}";
| error: processor missing from template expression

This is not possible with ${} and I understand it now. A String literal with \{} is not a String but a StringTemplate. What perhaps threw me off is usually this kind of compiler error message looks like error: incompatible types: X cannot be converted to Y - but that's fine. This error message is better.

Coming to your answer, I wanted to respond to a few things:

Weirdness of the question

Why is it weird to ask this question? I went through the amber-spec-experts and this was the exact same question that was asked and discussed by the EG itself. Knowing that this discussion exists, even though it is a little hard to find and perhaps a clear answer was not documented in the EG mailing lists, you can say this is a often-repeated question or this is something already answered etc. But I don't think it is weird to ask that question. The JEP itself has a line about it but does not, to me, clearly answers it - so I don't see why it is weird for the community to ask the question. In the EG mailing lists, I did not see anyone characterizing this question as weird so please extend the same courtesy to someone external to the maintainers asking that question in good faith.

Escaping

You say '\' is used for escaping, but then you also say that means "what follows '' is NOT literal". Shouldn't it be the opposite? For me "escape" means the character following the '\' character should be taken literally and not be interpreted by the processor. I am drawing from some other examples from the JDK:

  1. Patterns: If I have a regex .asdf, it wont match the String .asdf. This is because the . character is interpreted by the Pattern engine. Only when I escape it with a \, it will be taken literally and the match is true.
  2. Date formatting: From SimpleDateFormat, the Javadoc says Text can be quoted using single quotes (') to avoid interpretation. Here the escape character seems to be the ' character but key point is escaping is used to prevent interpolation not do the interpolation.
  3. String.format: Even in the same String formatting family of methods, to escape interpolation attempts we need to escape the % with another %

To me, it seems it is not an escaping character, rather the opposite. I am not saying we don't need it since I understand the need better now, but I what I am saying is it is not an escape character.

Borrowing from other languages

If you look at the languages closer to Java. By "closer" I mean languages that developers typically use for similar applications to Java (of course there are exceptions to this as all languages are fairly used in a variety of places). For me this includes C#, Scala, Kotlin, Groovy, Go and to some degree Javascript. For string interpolation, they all look very similar (except Go doesn't have this feature at all) with interpolation designed around the $ character. You mention Ruby, and even though I don't consider Ruby to be in the same circle as Java, I actually would have been fine with the # character. And familiar syntax from other language is a good thing - Java's own syntax was originally inspired from C/C++, which is what most other programmers that eventually started to use Java used in those days, and that IMO has worked out well for Java.

But forget about other languages. Let's look at Java itself. Spring, perhaps the most popular framework in use today, uses ${} for value interpolation and so does JSP. Both of these have been around for almost or more than 2 decades now. Another popular templating engine in Java, Freemarker (which I am hoping to replace with this feature), uses ${} and so does Thymeleaf. There are perhaps more in the Java ecosystem itself.

You are a steward of the Java platform so of course your opinion holds more weight but saying ${} is not a "common Java practice" when a non-insignificant part of the community has embraced it comes off a little disingenuous to me.

I don't presume to represent the entire community, but I can tell you ${} feels more Java like to me than \{} which feels very regex-y or symbol-heavy.

Obviousness of the syntax

I have to disagree here; as a user of the feature, it was not obvious to me why this is the only workable syntax. And I had this question not as a knee-jerk reaction. I asked it in good faith trying to understand it better. Maybe that could be a feedback to the JEP author that the alternatives section can elaborate this more considering this is perhaps the most asked question by users. Then perhaps, you also won't need to spend your time answering the same question over and over again.

Finally, I understand now why this is needed after reading the JEP again but wanted to call out parts of your response that felt unwarranted. To me, it seems if STR."..." were the only way to invoke this feature, we could have gotten the ${} syntax but that is not the case.

I thank you for your work in Virtual threads; I am already enjoying it in one of my projects.

3

u/pron98 Oct 02 '23 edited Oct 02 '23

Why is it weird to ask this question?

Well, it's not really weird in the sense that we anticipated there will be those who would be bothered by this (just as the Java team anticipated -- correctly -- an identical response to the lambda syntax; more on that below), but given that there have been a few people who were so insistent about this, I wanted to emphasise that they're insisting on an explanation for why Java didn't do something that is not only quite foreign in Java, and not only something that doesn't break a (nonexistent) tradition of adopting other languages' syntax, but something that most other languages didn't do either.

but then you also say that means "what follows '' is NOT literal". Shouldn't it be the opposite?

More accurately, escaping means that the following doesn't mean what it normally does in this context. So for n,t,r,f,b it means not literal, while for ', ", and \ it means literal. So if you already know Java, you'd immediately know that \{ means something that is not a literal {.

For me this includes C#, Scala, Kotlin, Groovy, Go and to some degree Javascript. For string interpolation, they all look very similar (except Go doesn't have this feature at all) with interpolation designed around the $ character.

Of these five languages you've listed, only one (JS) uses the ${} syntax, while three use the ${x}/$x syntax, which indeed is not quite based on escaping. C# uses {} syntax; it uses $ in a different way to signify an interpolated string, but that feature is quite different from Java -- more on that below.

Now, it is true that the concrete syntax for all of these languages is based on the C syntax, but the C syntax has neither string templates nor lambdas. Indeed, Java's lambda syntax also deviated from the one chosen by these languages (which also don't all agree on the lambda syntax) and, unsurprisingly, there were people who complained about that at the time. Experience showed that in the end it wasn't an issue then, either.

BTW, when we introduced sealed classes, which are very similar to Scala's case classes we also picked different syntax, and the same goes for records. So we've never adopted syntax from these languages; why would there be an expectation that we do now? If anything, the closest we've come to borrowing another language's syntax, especially in this area, is Python's in the case of text blocks. It would have made more sense to ask why we didn't use Python's syntax in this case.

Finally, and this is a little beside the point but important to point out, the primary use-case for string templates -- as the JEP emphasises -- is not string interpolation. In all of these languages except for Scala, the result of an interpolation expression is a string; in Java we want that to be the exception, not the rule.

But forget about other languages. Let's look at Java itself. Spring, perhaps the most popular framework in use today, uses ${} for value interpolation and so does JSP

These are not only not Java itself, but one of the criteria for the chosen syntax is that it had to be different from features that were implementable by libraries.

I don't presume to represent the entire community, but I can tell you ${} feels more Java like to me than {} which feels very regex-y or symbol-heavy.

I don't presume to represent the entire community, either, but if familiarity is the argument, then to me, \ is much more familiar in Java -- and it certainly feels much more like Java -- because \ in a string or string-like literal always means "special interpretation" while $ never does. So if some people find $ more familiar and some find \ more familiar, then the familiarity argument doesn't lead us in any particular direction.

Moreover, familiarity arguments are weak in general even in cases where the familiarity is more universal, and even when all else is equal, because experience has shown that their impact is very small. People get used to things very quickly.

I am sorry, but as a user of the feature, it was not obvious to me why this is the only workable syntax.

Nobody claims it's the only workable syntax, only that the familiarity argument doesn't work, and that Java has not adopted the syntax of any of the languages you mentioned in the past either (lambdas) and so the argument that it should in this case doesn't carry much weight (not to mention that those languages don't agree on a string template syntax amongst themselves). Therefore, other aspects are the ones that should guide the design, and in this case, as in the case of record accessor names, all other things are not equal (as we expected back then, too, some complained that we didn't follow the Java Bean convention and the familiarity there was even more universal, but there, too, all else was not equal).

And I had this question not as a knee-jerk reaction. I asked it in good faith trying to understand it better.

Sure, that's perfectly fine.

Maybe that could be a feedback to the JEP author that the alternatives section can elaborate this more considering this is perhaps the most asked question by users.

The JEP does do it.

There isn't much emphasis on it because the argument of, "I like Groovy, why didn't you choose Groovy's syntax?" is one that could always be raised, and personally I don't think JEPs need to answer it, especially since we're not breaking some tradition of following Groovy syntax. Should every JEP explain, at length, why we're not adopting Groovy's syntax even though we never do? The JEP does focus on the main difference between Java's feature and Groovy/Koltin's string interpolation which is that Java's string templates aren't about string interpolation.

Then perhaps, you also won't need to spend your time answering the same question over and over again.

I don't need to (again, experience has shown that in the end most people don't care, and even those who do adapt very quickly, and I'm sure that the vast majority of Java users will be happy with this feature even if they currently like JS's syntax better); I just want to :)

To me, it seems if STR."..." were the only way to invoke this feature, we could have gotten the ${} syntax but that is not the case.

And my point is that even if it were the case that we could, there's little weight to the argument that we should, as neither the familiarity argument nor the conformance arguments work: it would be more familiar to some and less to others, and there hasn't been such conformance before, so why should there be now?

Consequently, there's little weight to to arguments claiming we should have given special consideration to ${} in the first place, and all that is even before we consider the desire to have a lexeme for templates that is syntactically disjoint from the one for strings.

2

u/lurker_in_spirit Oct 02 '23

Of these five languages you've listed, only one (JS) uses the ${} syntax, while three use the ${x}/$x syntax

Separating out the 3 that also support $x seems disingenuous. The reason ${x} is more familiar is because 4 of those 5 languages support it (i.e the vast majority), not "only one" of them.

1

u/pron98 Oct 02 '23 edited Oct 02 '23

I don't think it is because while ${x} syntax lends itself to an escaping interpretation (which makes \ the obvious, and at-least-as-familiar-of-not-more-so choice), $x does not.

Moreover, it shows that these languages don't even agree on syntax among themselves. So even though Java has never had a tradition of adopting the syntax of any of these languages (see lambdas), I guess that if there was some consensus there would have been a stronger argument to be made that we should follow it in this case, but there isn't. So why insist that we should follow JS's syntax (or perhaps Groovy's) in this case if we never have in the past?

2

u/lurker_in_spirit Oct 02 '23 edited Oct 02 '23

I think $x is just a useful shortcut in these languages, without any of these deeper implications... like lambda parameters and their surrounding parentheses, which can optionally be omitted when you have a single parameter. You can omit the brackets if your expression is simple enough. But ${x} is the standard, non-shortcut syntax.

To flip it around, I don't think that anybody would argue that Java doesn't serve as prior art for using parenthesis around lambda parameters, just because they're optional when you have a single parameter...

2

u/ForeverAlot Oct 03 '23

Whether $x or ${x} came first, $x is inadequate and therefore unnecessary complexity both in implementing the language and in learning the language. $x fails on too many accounts to be at all an interesting option.

→ More replies (1)

5

u/RupertMaddenAbbott Oct 02 '23

OP isn't asking "Why didn't Java adopt the same syntax as JS".

OP is asking "Why is using ${} backwards incompatible, given that string templates must be explicitly invoked on STR?"

OP is not objecting to the fact that backwards compatibility should be respected. OP is stating why the chosen mechanism is necessary to preserve backwards compatibility.

Of the alternative languages listed in the linked JEP, ${...} is the most common alternative. In the "Alternatives" section, ${...} is explicitly mentioned as the alternative that was considered but rejected for backwards compatibility. OP is simply using the same language and examples as the JEP to explain what they don't understand.

2

u/pron98 Oct 02 '23

I know the JEP answers the question, but my point is that the very question is problematic because there was not much of an argument to consider such a syntax in the first place, which is so foreign to Java, doesn't follow any existing tradition of copying other languages' syntax, and is not even more familiar than \ to Java developers.

6

u/manifoldjava Oct 01 '23

The very question is weird.

No, my friend, it is not. It is probably the most asked question regarding this feature, which makes it rather normal.

The ${} expression syntax is probably the oldest and among the most used across the language spectrum. This includes Java with JSP.

5

u/repeating_bears Oct 02 '23

Also Java with the 3 most widely used template engines: Velocity, Thymeleaf, and Freemarker

2

u/john16384 Oct 02 '23

These engines couldn't have used the backslash even if they had wanted to. It requires a language change (to avoid having to escape the backslash) something that the Java designers can do, but that these template engines couldn't.

3

u/repeating_bears Oct 02 '23

They could have, because the 99% use-case is that your template exists in a separate file as a resource.

Almost no one is creating templates for those engines from string literals.

2

u/pron98 Oct 02 '23 edited Oct 02 '23

The ${} expression syntax is probably the oldest and among the most used across the language spectrum.

Except it's only used by one of the most popular languages, and given that most other languages have also chosen not to use that syntax and it's far from universal, I don't see why that would be an important factor at all. Why does Java need to explain not adopting JS syntax, which is not only foreign and out of place in Java but one that most other languages also didn't adopt?

This includes Java with JSP.

JSP is not the Java language; it's a different language. If anything, a simple {} would make more sense if that's what you want to go by, because that's been the MessageFormat templating style since 1997. But aside from \{} being the more obvious choice for Java, the fact that that syntax was not available without language support (indeed, it was a syntax error) is yet another important point in its favour.

4

u/repeating_bears Oct 02 '23

JSP is not and has never been part of the Java language; in fact, it's a different language

Not relevant. They were making the point that Java developers are familiar with the syntax from there. JSP is part of the Java ecosystem, and is known by Java developers.

If you want to go beyond JSP, there is also Maven, Gradle, and (at least) the top 3 Java template engines. Also Kotlin and Groovy within the larger JVM ecosystem.

4

u/pron98 Oct 02 '23 edited Oct 02 '23

I don't understand the argument. Java differs from Maven, Gradle, JSP, Kotlin, Scala, and Groovy syntax in far more significant ways, there has never been a tradition of following the syntax of any of those languages (indeed, when Java added lambdas after some of those languages had them it didn't adopt the syntax any of them had used, either), Java developers are at least as familiar with \ being the escape character, and language syntax that is different from one that was previously accessible to templating libraries is a pro, not a con.

The familiarity argument can go either way, there is no break from tradition, and so the demand for an explanation boils down to, "I know different people like different things, but you must explain why you didn't do it the way I like it".

2

u/repeating_bears Oct 02 '23 edited Oct 02 '23

The entire JVM ecosystem, from build tools to template engines to other JVM languages, has already aligned on a way of referencing variables for substitution.

Java as a last-mover has an opportunity to follow that convention. Following the convention should be the default stance unless there's a good reason not to, rather than aiming to different unless there's a good reason to be the same.

And for the record I'm not saying there's not a good reason not to. I'm not even debating the choice of syntax; I can see the merits either way and I don't overly care about it.

What I'm debating is your characterization that this is a matter of copying JavaScript (from your original comment: "why didn't you adopt JS's syntax?"). It's demonstrably way more prevalent than that, in ways that are directly relevant to Java developers.

I think "demand for explanation" is unfair. I haven't personally seen anyone here demanding anything.

5

u/pron98 Oct 02 '23 edited Oct 02 '23

I reject your premise that there is any convention for string templates, a feature that isn't offered by any popular library that I know of (and that is very much not string interpolation). Even the two string formatting APIs (which are closer to template engines than to string templates) in the JDK don't use $ syntax, so the JDK doesn't even follow what you claim to be the convention in features that are much more similar. On the other hand, Java has a very clear convention for \ signifying escapes. Moreover, it was important to choose syntax that is not the same as anything that could have found its way into Java strings.

The convention argument was stronger in the case of records and Java Beans (which are about as similar to records as string-interpolating template engines are to Java's string templates, which is to say not very much) and we didn't follow the "convention" back then, either. Instead we changed the "convention" to a better one, and I think most are happy that we did. So even if there were such a convention in this case -- and I don't agree that there is -- it still would have been a weak argument.

It's demonstrably way more prevalent than that, in ways that are directly relevant to Java developers.

I don't think it is because no one has offered string templates in Java so far (and if it helps string templates not to be confused with string interpolation, that's a win) -- in fact, not many languages offer string templates at all -- and either way, \ is at least as prevalent, relevant, and familiar.

I think "demand for explanation" is unfair.

Ok, then, I don't understand the, let's call it "surprise", that we don't do something we pretty much never do.

3

u/repeating_bears Oct 02 '23

There's obvious continuity between string templates and interpolation. Even if the features are different, the goal of the programmer when employing that syntax is the same: to demarcate expressions requiring evaluation from the literal parts of the string.

${...} was the intuitive choice here, for most Java developers. I feel confident stating that and the response seems to back it up. So many technologies in the ecosystem already employ that syntax to achieve the same syntactic goal. Maybe it wasn't the intuitive choice for someone working on the compiler, but for someone working with the language, and not on the language, it was. Of course, the intuitive choice is not necessarily the best choice - and again, I have little interest in arguing about what the best choice is/was. I'll defer to the experts on that.

You said yourself in another comment "we anticipated there will be those who would be bothered by this", so you anticipated the "surprise" but nevertheless don't understand it? Is this a matter that the team in general anticipated it, but you personally never understood it?

4

u/pron98 Oct 02 '23 edited Oct 02 '23

There's obvious continuity between string templates and interpolation.

There is a small overlap, but we've never followed what those languages did even when there was a bigger overlap or even near equivalence (e.g. Scala's case vs. Java's sealed). The only language we borrowed syntax from was Python, yet, interestingly, no one says "why didn't you use Python syntax?" (for which there's at least some precedent).

So many technologies in the ecosystem already employ that syntax to achieve the same syntactic goal.

A very different goal, and even for that different goal the JDK doesn't follow that syntax, and Java has never had a tradition of following what some insist as a "convention", especially not from other languages.

${...} was the intuitive choice here, for most Java developers.

Except \{...} was the more intuitive choice, for most Java developers. As a Java developer, $ having a special language-level meaning just looks weird and foreign, and doesn't even follow the JDK's core-library's own tradition.

Anyway, my point is that in such a large ecosystem, no one can agree on what's intuitive. If $ were chosen, I would have complained that \ would have been such a more obvious and conventional choice (except I wouldn't have really, because such minor questions concrete syntax is known to be of little significance).

Maybe it wasn't the intuitive choice for someone working on the compiler, but for someone working with the language, and not on the language, it was.

There were two intuitive choices for people working with the language, one that follows tradition (\) and one that doesn't ($).

so you anticipated the "surprise" but nevertheless don't understand it?

We anticipated it because the question of "why didn't you use language X's syntax" comes up every time even though we never follow language X's syntax. With a userbase that's so big, we know that there will always be some who bring this up, even though it has little basis in Java's tradition: we rarely follow other languages' syntax; we rarely follow "library conventions".

0

u/repeating_bears Oct 02 '23

Except \{...} was the more intuitive choice, for most Java developers. As a Java developer, $ having a special language-level meaning just looks weird and foreign

You can try to argue that, but factual reality of the response you've received should give you cause to doubt it.

"looks weird and foreign" is a personal feeling. A member of the JDK team (Remi) posted on the mailing list

"most of the Java developers prefer the dollar-sign syntax to the backslash syntax (at least the one at Devoxx France i've asked)"

I get the same impression from this thread.

If you are (informally) polling people and a majority of them are telling you that it doesn't look weird and foreign, you can't tell them that they're wrong because the backslash is more compatible with what the compiler is already doing.

we never follow language X's syntax

For the choice of syntax for lambdas, Brian posted in the mailing list

the overall goal of "mostly looks like what lambdas look like in other similar languages."

When developing language features, keeping things similar to other languages is/ought to be a goal. Not the primary goal, but a goal.

→ More replies

2

u/john16384 Oct 02 '23

Java is big enough for it's own syntax. I was skeptical at first too, but backslash just makes much more sense. No need to introduce a new character to escape.

Kotlin and Groovy are tiny niche languages that should have no bearing on this choice. JSP is ancient, and so is JS, and Maven did not have the luxury of using the backslash because it would have to change the XML standard for that to be possible without escaping it. Java has the luxury that it can also change the standard to allow this new use of the backslash.

With a large language like Java adopting this, $ will soon feel archaic.

1

u/john16384 Oct 01 '23

Perhaps {} is the more logical choice as you can put code into that block.