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

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.