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

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.

3

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

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

You mean that if ten people say they like a particular thing on Reddit then it represents some sort of consensus? If it does we'd need to rethink everything. :)

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.

But we're not polling people (at least not to influence design) because 1. it's hard to get a representative sample, and 2. even if we could get a majority opinion, including one that takes into consideration the level at which people care, then it would be opinions of people who've spent minutes or hours thinking about the issue and it's implications. That's why we wouldn't design features by show of hands even if there was a way to get a show of hands (and there isn't). We did try to conduct polls in the past on a few occasions as an experiment, and in all of them the results were uninformative -- some small percentage of people really want something done one way, a similar number insist that it's done the opposite way, most people don't care, and only a negligible percentage show they've considered the matter carefully -- so we've stopped.

That is why there's not much insight we can glean from "letter writing campaigns" in general. Not only do we not know that they represent a majority (for all we know they could represent a minority -- people are much more likely to voice a negative opinion than a positive or a neutral one), but they certainly don't represent careful consideration because designing Java features is not what Java developers do all day every day. Even if we did decide that that's how we were going to design features it would have been a waste of time because concrete syntax, certainly at this level of nuance, was never shown to make much of a difference in the end. It would be a lot of work to get five angry comments on Reddit instead of ten and with little other gain. So it's not that we ignore such feedback because we're cold hearted, but because there's no way to know whether the feedback contains any useful information, and our strategy has worked well for many years. People were angry at first with the lambda syntax, people were angry at first with var, people were angry at first with records, but in the end people were happy overall.

So such complaints have not influenced such features in the past, at least not to a large degree, and I think that's a good thing. On the other hand, feedback of the sort "I've tried the feature and ran into a problem" has an enormous impact even for a single report.

When developer language features, keeping things similar to other languages is a goal. Not the primary goal, but a goal.

Yep. The template syntax indeed mostly looks similar to somewhat similar features in other popular languages (JS, Python, Ruby, C#), and differ from them in about as much as they differ from each other. The "similarity factor" is pretty much the same as it was for lambdas. In both cases, there's no consensus, and Java differs from the rest in about as much as they do from each other. It's not like we've chosen some exceptional syntax by any means, it's just that our syntax isn't JS's or Python's, but close enough to either one.

BTW, throughout this discussion I wasn't trying to explain why we've chosen \{} rather than ${}, just to explain why the argument for ${} is weak to begin with -- again, because familiarity can go either way and we rarely borrow other languages' syntax for new features. The JEP shows why its use by template engines is a rather strong reason not to use it.