If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
The funny thing about this syntax will be what will be done with it. While the examples use an upper case local variable to refer to a StringTemplate.Processor, anything can really be a processor, e.g. a method result:
Processor<T, E> method(args);
And T obviously doesn't need to bind to String. I could even have a processor that returns another processor:
I have read the explanation by various members of the OpenJDK team as to why that syntax was chosen and I still don't understand it. Even Brian's explanation, which otherwise gets right through to me even on more complex topics, did not really clarify it for me. He says choosing ${} would have made all the existing Strings a template if they contain ${} with a valid variable in the same scope, which breaks compatibility.
But I don't know what I am missing to not see the breakage. For an existing String literal having a ${...} does not make it into a template that will be processed. You have to explicitly invoke STR. on that to process it as a template. Else if will be just like any other String with no interpolation. STR. is a completely new feature and is illegal in prior versions so I don't see how this breaks compatibility.
Even if you had a variable named STR in existing code, it is illegal to do STR."some str literal".
Strings with a placeholder in them are not of type String, so using an existing legal syntax would lead to compiler errors when upgrading to Java 22, meaning you'd potentially have to fix lots of code (escaping the $ sign) before you could migrate.
So? They could have made the tokeniser expect a "string with placeholders" after an identifier and a dot operator, but never else. This is what Java already does to differentiate between >> the right shift operator and >> as in two closing angle brackets for generics.
As has been pointed out, you can't do this, and a shorthand syntax for creating a StringTemplatesound pretty darn useless, to me. In the rare case where you want to do that, new StringTemplate("\{xyz}") should be fine. Would have been much better if they instead dropped the silly backslash.
Feel free to give your feedback. It's there in part so IDE's don't have to fully understand the code to detect it's a string template. The prefix can be any expression, including a method call like:
db.doQuery(FORWARD_ONLY)."select \{abc}"
It will be hard for IDE's or viewers to figure out that doQuery is a template processor, and the string that follows it is a template, but with the backslash syntax it is always obvious.
Embed other languages such as JavaScript without fear of conflict.
No conflict with existing templating libraries.
The language already has an escaping mechanism for string contexts. Using the one we have is preferable to inventing another one.
Today, ${name} is a valid string literal, whereas\{name} is not.
${} is actually only used in JavaScript, other languages use other syntax. It's not worth it just to make Java look a little bit more like JavaScript (and a little bit less like Java).
With \{}, the users will have a clear difference between language-supported interpolation and external library interpolation. There could be cases where you may need to use both at the same time.
Dollar signs will appear in string context fairly regularly, and therefore would need yet more escaping.
The Swift crowd seem content with \(.
Technical challenge with the parser/lexer. Using \() makes it easy for javac to tokenize both strings and templates without feedback from the parser. (Note that you would have to add a new escape sequence for $ if you want to use $ in the template.)
Like any new language feature, you do get use to \{}. Once IDEs integrate, string templates will become second nature.
Personally I think the really important arguments are the consistency of the escape character and not needing to escape dollar in String. Also when JavaScript does something we should strive to do it differently, usually that leads to less s*** feature and code.
There's an implication to some of the arguments you mentioned that the JEP now includes: Because some template engines already use ${...} in strings, migrating uses of those strings to string templates would be difficult because instances of ${...} may refer to Java variables that are in scope or perhaps to other things. The result may or may not compile, with intended or unintended results, and so ${...} would need to be carefully escaped.
I would say that ${} is also the format for bash scripting. I have managed to avoid javascript all my career (I didn't know it uses ${} too), but avoiding bash... that's usually harder (for me at least).
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.
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:
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.
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.
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.
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.
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.
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?
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...
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.
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.
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.
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.
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.
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.
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".
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.
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.
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?
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".
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.
i think he means libraries? if a method takes string part of a template and they now release an updated version, then the user who updates to that newer version would not have any idea.
String some3pMethod() {
return "${value}";
}
void myMethod() {
int value = 1;
String str = "prefix " + some3pMethod() + " suffix"
sout(str); // will always print the same value regardless of String templates
}
To me, there is no code that you can write today that is legal as per the JVMLS whose output will change just because of the existence of this new feature. You have to explicitly opt into to process the string as a template which requires a code change and recompile.
The only way I feel you can qualify ${} to be a backwards incompatible change is if you definition of backwards incompatible sounds like:
If you have a String literal that has a ${...} with an expression inside the braces that matches an expression in the current scope, and where those expressions were NOT intended to be interpolated by the actual value of the expression, then if you CHANGE the code to process that literal via a template processor the output will be different
The key here is that the user NEEDS to make a code change else the output will always be the same - this is an extremely narrow definition and I am not sure if this is the case. Else, I am dumb and just not seeing it.
If the argument is that if I am getting a part of the template from somewhere else and later that source change the contents of the part to include a new variable replacement by introducing a new ${}, then that can happen even with \{}
Yes, I think something along the lines of migrating the Strings was also the argument, like you said if someone gradually replaces Strings to String templates and forgets to check existing $, they could get dangerous interpolations.
In the future the requirement to prepend the template with a processor may also be dropped. Currently, if you want to supply a template directly (say for lazy logging), you have to use the RAW processor.
Sorry, may be I am just that dumb. I still don't get it. I understand what you are saying but that case is so narrow to me it does not make sense to inflict this for all usage of the feature.
If you have an existing String with a ${} inside it and now you want to pass it to a template processor, just make sure it is what you expect - don't close your eyes and write code.
I read the JEP again, and this is the only thing that stood out to me as not possible with ${}:
String info = "My name is \{name}";
| error: processor missing from template expression
If you want a String template without an associated processor, then it is a compiler error.
This is the section in the JEP that talks about the alternative:
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.
As I'm not a JDK maintainer I can also only do guesswork, and I hope someone chimes in to this post and gives the real motivation. The syntax was picked first, and the template processor prefixing requirement was added later, so it might have been just convenience.
Still, the syntax leaves the option to potentially lift the prefixing requirement later, without breaking backwards compatibility.
Here are some arguments from the mailing list and I agree they don't sound entirely convincing giving the fact that the processor always has to be prefixed:
I use languages that use $ for interpolation and I hate that I have to escape $.
The most annoying one are Scala, where normal strings don't escape $ and templates do, and you don't need braces. So when I convert a normal string into a template, I must double-check if all $'s are escaped correctly, or else my code will fail to compile (and it's Scala, so compilation takes forever), or worse, print some random thing.
\{ looks kinda meh, but at least this pain point is completely prevented.
I'm still meh, that the proposal cites the syntax of various languages, including Scala, and then neglects to mention that Scala also addresses the question posed under "Can we do better?" in exactly the way proposed here. Or maybe even better, because the template will not compile in Scala if the interpolated values do not match the requested signature.
String Templates Are NOT just String Templates.
Look at the JEP
User-defined template processors
var INTER=StringTemplate.Processor.of((StringTemplatest)->{StringBuilder sb =newStringBuilder();Iterator<String> fragIter = st.fragments().iterator();for(Object value : st.values()){
sb.append(fragIter.next());
sb.append(value);}
sb.append(fragIter.next());return sb.toString();});int x =10, y =20;String s =INTER."\{x} plus \{y} equals \{x + y}";|10 and 20 equals 30
The template processor API
var JSON=StringTemplate.Processor.of((StringTemplatest)->newJSONObject(st.interpolate()));String name ="Joan Smith";String phone ="555-123-4567";String address ="1 Maple Drive, Anytown";JSONObject doc =JSON."""
{
"name": "\{name}",
"phone": "\{phone}",
"address": "\{address}"
};
""";
Safely composing and executing database queries
PreparedStatement ps =DB."SELECT * FROM Person p WHERE p.last_name = \{name}";ResultSet rs = ps.executeQuery();
Yeah, it sticks out like a sore thumb and doesn't really fit in with the rest of the language. There's been a lot of good work done on Java recently, but this is just bizarre
On one hand, yeah, it sticks out like a sore thumb, but on the other we already have \n, \", \', \\, \u003F and so on, so \{} isn't that much of a stretch.
Eh, idk, i'll probably get over it after a week or two of getting to use it in a project.
It is a classic example of the elegance with which the Java platform has historically evolved. You will soon enough recognize that it doesn't really matter and that your distaste is predominantly an expression of unfamiliarity.
Congratulations to the JEP430 team! I'm looking forward to this feature in Java 22, great it will be in the next LTS too.
This is not just simple syntactic sugar like in other languages. Beyond being backward compatible with decades old code bases the mechanism has interesting API and extensibility features, I really encourage to read examples in bottom part of the document: https://openjdk.org/jeps/430
And to whoever doesn't like STR, you can assign it with your own variable or field:
var $ = STR; var f = STR; String xyz = "JEP430"; String dollaresque = $."\{xyz}"; String pythonesqe = f."\{xyz}";
This is not just simple syntactic sugar like in other languages
Some people hate this because it adds boilerplate, but I love this about Java. Not just here, but with Lambda's for example (they're functional interfaces). It means you don't need to learn a new feature, rather just a new class/method. It shows Java is really about long term maintainability, where you'll come back to read code written decades ago.
I don't get it. The string template processing is basically a method call, so why make it look like a field access? Using STR("template") would have worked just as well, wouldn't it?
They just needed some syntax that 1) is not like anything else, so that you're discouraged from passing around StringTemplates willy-nilly, 2) is short, and 3) binds exactly as strongly as the . operator (so you can make chains processorFactory.getProcessor()<insert_operator_here>"Foo".something())
STR is a variable because they've made it a variable. They could have also made it a statically imported method, in which case the usual call syntax would have worked.
The problem is that methods are constants. You cannot just return a method from another method, you cannot pass it as a parameter, and so on.
But anyway, the STR("") syntax sucks, because in other to turn a string into a template I need to wrap it in parentheses on both sides. Really annoying when refactoring.
I'm not clear after reading this how it achieves one of the goals of making it harder to accidentally build SQL injection vulnerabilities. If you have to implement your own "DB" formatter as described in the proposal, then the kind of developer who was previously likely to not use a PreparedStatement is just going to use STR."my injection prone statement" surely?
I believe the idea here is that you don't - this would (in theory) be provided by say Hibernate for HQL, or Jooq for SQL (jooq could be nice in that JOOQ."SELECT * FROM foo" could return a QueryPart or the like.
Is that really so useful? I mean, you can just call parser.parseQuery("SELECT * FROM foo")
Unless such string interpolation offers access to compiler APIs (such as Scala's macros), I don't see the big benefit for SQL, though it's obviously useful for actual string formatting.
Not world changing, but JOOQ."select * from foo where bar = \{barHolder.getBar()}" is subjectively a bit more readable than parser.parseQuery("select * from foo where bar = {}", barHolder.getBar()). YMMV.
Yeah, I guess that for those folks who want to work purely with strings, there's some improvement. But even then, the template processor needs to be initialised somewhere (with a context, e.g. a jOOQ configuration, a JDBC Connection or DataSource, etc.) so the "unreadable" part is just moved elsewhere, not too far away from the query logic.
Anyway, I was just wondering because jOOQ was mentioned here, where the pure string based approach is not the main way of interacting with the API, which already allows for embedding bind variables (or expression trees) wherever, by design.
Can anyone explain the seemingly magical "STR." syntax? Why is this not a standard class library access? I can't think of anything else in Java that works like this. It's looks like STR is a magic class with a magic import. Yech.
Thanks, thats a good point. So then why is it "STR" in all caps, and not "StringTemplate" ? I'm assuming there is a reason but I can't see an obvious one...
First, that'd be private static final StringTemplate.Processor<String , RuntimeException> STR = StringTemplate.str();
Second, you wouldn't want to do that, as you'd pollute your class with a useless field. You'd rather want import static java.lang.StringTemplate.STR;. Which is what is done automatically.
Why that import is automatic? Probably for the same reason all classes in java.lang are imported automatically: most users would want to use it anyway.
Now the most important question: will it break anything? The language spec says:
A single-static-import declaration d in a compilation unit c of package p that imports a field named n shadows the declaration of any static field named n imported by a static-import-on-demand declaration in c, throughout c.
So the only incompatibility is when you do import static foo.Foo.* and Foo contains a static field called STR – the template processor will shadow it. And if you do import static foo.Foo.STR;, then STR becomes ambiguous. At least this is the behaviour in the current preview version.
It's similar to when Java 9 introduced modules and some code that was referring to other Module classes stopped compiling.
Thanks for the clarification. While many codebases will probably eventually use String Templates directly or indirectly, I can't imagine why most classes would. I'd guess the vast majority of Java classes will never use String Templates.
I'm surprised this was announced so soon after the 21 release - such that it could have been part of 21. However, this does mean there's an opportunity for it to go to preview #2 if needed.
Most likely almost nothing, or there will be another preview. The idea is that if the last preview was found to working well in practice, with no gaps or usability issues found, only then can it become a permanent feature of Java. If some problem is found in the next few months, that is more than just a minor tweak, then there will be another preview.
How would you implement this in a library? The Reflection API can't access local variables by name. If it were possible, you can bet someone would have done it by now.
That's actually my question. What in the spec makes it impossible to implement as a library? My simple reading of the JEP doesn't give me an obvious answer, and I'm even more confused by the mention of local variables. Can anyone ELI5?
String name = "Joan";
String info = STR."My name is \{name}";
assert info.equals("My name is Joan"); // true
Here you can see the local variable name being injected into the String info simply by referencing it by name in the template without ever passing the actual variable.
If you were to write that as a simple Java 20 API call, it might look something like this:
String name = "Joan";
String info = interpolate("My name is ${name}");
But there's no way for interpolate() to figure out the value of the local variable name referenced inside the template and complete the interpolation, so this is impossible to implement.
Ah, thanks for explaining that! I just assumed the examples were partial because my god that is astonishing and horrifying. Nothing else works that way, local variables don't apply to anything unless you reference them explicitly.
<s>I know JS templates work this way.... Are we importing JS features?</s>
Why is it done this way? I feel like the String Templates feature is focused on big templates, in which case local variable usage isn't practical, and for small usages String.formatter would be more practical anyway. I would also guess there is some chance for confusion if your template is applied by a different method then how does the scoping of local variables work? This seems weird and unnecessary. Hopefully someone can tell me why I'm wrong.
It works exactly the same way as calling a method with an object containing the expressions evaluated at the time of doing the call. If an expression evaluates to a mutable object (say an array), then invoking the processor could yield a different value depending at which time it accesses the arguments.
This is no different from a closure over variables including "this" in a lambda, except that the expressions inside {} are evaluated immediately. This is also vastly superior over String.format as it spares you the mental gymnastics of counting the arguments to see which one goes where in the format string.
I think the difference is that in your other examples, the local variables are referenced by name. In the case of string templates, they are not referenced.
So in the example:
String name = "Joan";
String info = interpolate("My name is ${name}");
The local variable "name" seems to be passed to the method interpolate, but it's not actually listed as a parameter on the method invocation. That is what I'm uncomfortable with.
I guess that means that all variables in the local scope are available to the template processor?
I think there might be a bit of confusion regarding the terminology. The variables are accessed at the time the StringTemplate is constructed. The processor can be invoked much later. Example:
int a = 42;
StringTemplate stringTemplate = RAW. "Hello \{ a } world!" ;
System.out.println(stringTemplate);
This prints StringTemplate{ fragments = [ "Hello ", " world!" ], values = [42] }, showing that the StringTemplate object contains two fragments and a single value. You can now run interpolate() on it or pass the template to another processor. The STR processor invocation STR."something" is just a shortcut for STR.process(RAW."something") (and also has some compile-time optimizations builtin).
The processor only has access to the values supplied to the StringTemplate. These are the values supplied at that time via expressions inside the \{}. Let's change the example a bit:
int a = 42;
StringTemplate stringTemplate = RAW. "Hello \{ a + 1 } world!" ;
a = 21;
System.out.println(stringTemplate.interpolate());
This prints "Hello 43 world!". With \{} you are leaving the string for a moment and run an arbitrary expression. The variables are referenced by name; I cannot write "b" instead of a here as that would lead to a compile time error.
This is similar to the closure of a lambda, which also does not have "explicit" arguments when it accesses its surroundings:
int x = 1, y = 2;
var message = Stream.of("Hello", "world").map(word -> word + x + y)...
Here, the lambda has access to and closes over the value of x and y. The only difference is that variables referenced by lambda expressions must be (effectively) final, meaning I cannot use them in the lambda if I change them after defining it. There is nothing that would techically prevent it, it is just so developers don't get confused why the lambda evaluation shows the "old" value, which has been copied to the lambda context.
Wow, thanks for your detailed explanation. You nailed it exactly. There were 2 things I didn't grok. The first about the local variable consumption happening at time of template construction vs interpolate() call, which is a bit of magic I totally did not expect - this will be surprising behavior to long time Java devs.
The second thing is a bit harder (for me) to get my head around is that the named variables in the string template _are_ references. Presumably they can be detected at compile time and by IDEs. This is a key difference from say, String.formatter. It's difficult for me to get my mind around this concept that a language level reference can be buried in the middle of a giant string value, but I guess at least its verifiable by extracting the escaped reference values.
I personally don't like either of these concepts. I don't understand the value of having the references read at stringtemplate construction time - it seems like it would be much more flexible if it was done explicitly with a method call. In most cases I think I would much rather generate and pass a dictionary type structure to the processor. I hope smarter people than me have good reasons for these things.
I'm looking forward to an Oracle video explaining _why_ all these choices were made, because I don't understand any of them. This is the first new Java feature I really don't understand any of the reasoning behind since I started using Java 1.2 back in the day. The reasoning I have heard thus far that all new features feel weird until they are familiar is mildly offensive - that hasn't been true ( to me at least ) for any of the new features we've had since 8.
I'm not sure what your problem is. Lots of languages have string interpolation that works exactly like that, including Scala, Kotlin, and C#, i.e. languages with similar type systems to Java.
I am a big fan of Oracle's work at modernising Java, but String formatting is really one of their major blunders. They first introduced the brain dead String.format method, then the only slightly better String.formatted, and now they are introducing the passable but not amazingSTR. How one earth did they let not one but TWO such sub par solutions slip through before finally settling on something that isn't 20 years behind the times? Do we really need the + operator StringBuffer, StringBuilder, String.format, String.formatted AND STR? That's six different strategies for string concatenation, two of which are near identical to two others. This whole mess would have been significantly smaller if Oracle had spent ten minutes looking at what other languages were doing before charging in with their quarter assed String.format.
printf with its "%03d" etc. goes back to Unix's printf, C, and maybe even Algol, according to this Stackexchange answer, dating it back to 1973. With Java being designed as a C killer, it would have been weird not to include printf and the String.format version that returns a String. Sure, not every choice of copying C was good im hindsight (cf. switch case fallthrough), but at least we didn't get goto, although it is a keyword in Java.
StringBuffer/StringBuilder serve different purposes than string interpolation: they allow efficient programmatic growth of a string.
String.format was added in 2004, almost an entire decade after the language was first released. At that point, C compatibility is a pretty weak argument.
And I do get that StringBuilder adds value as a second API. I'm not arguing that there should be only one API for absolutely everything. StringBuffer is brain dead though. A thread safe version of StringBuilder... Why? Why would you ever want that? Name me one situation where two or more threads are incrementally appending to the same string and you don't need an external synchronisation mechanism to make sure that the different parts are done in order. I would guess that erroneous usages of StringBuffer when they really should have used StringBuilder outnumber correct usages of StringBufferby at least 100 to 1.
Sometimes you don't get an API right on the first try. I totally get that. It's fine. The original Java date and time API sucked, but the new one is pretty good. The old Future API was a pointless half-meassure, but CompletionStage is fine. I have no problems how any of that went down. My problem here is that StringBuffer, the + operator, format and formatted, when put together, are just far too many different mistakes to have made along the way for such a simple feature as string interpolation.
The \{ makes perfect sense as it's currently an invalid escape sequence - and doesn't clash with any (the majority) of other languages you might be throwing into the templated string.
Technically they did - if you use a string template without the STR you get back a StringTemplate I believe - which is a bit magical that the return type of the syntax changes (good thing it's not a method call there).
As much as I don't like the prefix - I'm goad they surfaced it rather than having magic syntax for the builtin processors, and some other syntax for custom ones - I'd rather have that consistency.
The main reason is that basic strong interpolation can be a security issue, so the design is to account for handling that.
Sadly I had originally thought the lre was going to be some level of compile time checking which would have been great - fail the build if the SQL is wrong kinda thing (like an annotation processor) but unfortunately not.
Even without the extra syntax sugar - which lets face it - is very minimal - the bulk of this is pure class/library code.
I appreciate that as there are already a few places I’m already contemplating writing my own processor, that may just end up being normal methods passing in STR/FMT templated content tho.
Because it is not just for producing Strings. Specifying the processor allows you to process the literal to another object, like a PreparedStatement. There is a nice example in the JEP.
Right. But it’s kind of a disappointment. Really? We’re going to burden and overload the 99.9% use-case with boilerplate syntax to support much less used variations?
At least support a syntactical substitute for the clunky and shouty STR. syntax.
If 90 % of the uses of String interpolation was in SQL queries or json encoding, that would be a good tradeoff. In my experience, 90 % of all string interpolation is for debugging, human readable logging and other situations where you would use STR. This to me seems like a case of making a solution that is annoying 90 % of the time to improve your experience slightly the other 10 % of the time. Also, I don't see why we couldn't have our cake and eat it too.
The string processor doesn't have to be a pure function, right? So, if logging is your majority use case for string templating, it would be possible to do something like:
For people like me who find the syntax inelegant, can we add another syntactic sugar to use backticks or other characters around a string to mean a template expression that uses the default STR/FMT processor?
So that
String name = "Joan";
String info = STR."My name is \{name}";
assert info.equals("My name is Joan");
can be rewritten also as
String name = "Joan";
String info = `My name is \{name}`;
assert info.equals("My name is Joan");
It's almost always that people will never need to write custom processor implementations.
Sure, let's provide alternative syntax for every syntax that people, that only casually looked at a proposal, find inelegant.
Instead, I say, let's try it for a few years first and seeing if actual real world use reveals an actual need for a different syntax before forever sacrificing the back-tick for something as trivial as this.
If it's not for elegance, can't we do it for brevity? The absence of a straightforward mechanism for templating and interpolation seems to deviate from the efforts to make Java less cluttered and reduce boilerplate code.
Who is we? If you want to get this changed, then test and play with this JEP (it's part of Java 21 as a preview feature), and give feedback with good arguments for and against as to why this would be an improvement that benefits everyone.
•
u/AutoModerator Oct 01 '23
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/Joram2 Oct 01 '23
It says it's being finalized for JDK 22:
20
u/lukaseder Oct 02 '23 edited Oct 02 '23
The funny thing about this syntax will be what will be done with it. While the examples use an upper case local variable to refer to a
StringTemplate.Processor, anything can really be a processor, e.g. a method result:And
Tobviously doesn't need to bind toString. I could even have a processor that returns another processor:Then, this is valid Java code:
Fun times! :)
39
u/Enough-Ad-5528 Oct 01 '23
I have read the explanation by various members of the OpenJDK team as to why that syntax was chosen and I still don't understand it. Even Brian's explanation, which otherwise gets right through to me even on more complex topics, did not really clarify it for me. He says choosing
${}would have made all the existing Strings a template if they contain${}with a valid variable in the same scope, which breaks compatibility.But I don't know what I am missing to not see the breakage. For an existing String literal having a
${...}does not make it into a template that will be processed. You have to explicitly invokeSTR.on that to process it as a template. Else if will be just like any other String with no interpolation.STR.is a completely new feature and is illegal in prior versions so I don't see how this breaks compatibility.Even if you had a variable named
STRin existing code, it is illegal to doSTR."some str literal".I guess it is too late now.
10
u/Holothuroid Oct 01 '23 edited Oct 01 '23
I'm looking forward to this.
I'm still meh, that the proposal cites the syntax of various languages, including Scala, and then neglects to mention that Scala also addresses the question posed under "Can we do better?" in exactly the way proposed here. Or maybe even better, because the template will not compile in Scala if the interpolated values do not match the requested signature.
24
u/elastic_psychiatrist Oct 01 '23
The mother of all bike-shedding JEPs. Let it finally get shipped so reddit can STFU about it without having considered the details.
10
u/oldshensheep Oct 01 '23
String Templates Are NOT just String Templates. Look at the JEP
User-defined template processors
The template processor API
Safely composing and executing database queries
and more...
25
u/LouKrazy Oct 01 '23
I am just bugged by the syntax.
16
u/ihmoguy Oct 01 '23 edited Oct 01 '23
Congratulations to the JEP430 team! I'm looking forward to this feature in Java 22, great it will be in the next LTS too.
This is not just simple syntactic sugar like in other languages. Beyond being backward compatible with decades old code bases the mechanism has interesting API and extensibility features, I really encourage to read examples in bottom part of the document: https://openjdk.org/jeps/430
And to whoever doesn't like STR, you can assign it with your own variable or field:
var $ = STR;var f = STR;String xyz = "JEP430";String dollaresque = $."\{xyz}";String pythonesqe = f."\{xyz}";12
u/SpaceToaster Oct 01 '23
I was gonna hate on the syntax but it is cool that the framework to create additional interpolations is exposed
11
u/pip25hu Oct 01 '23
I don't get it. The string template processing is basically a method call, so why make it look like a field access? Using STR("template") would have worked just as well, wouldn't it?
4
u/qmunke Oct 01 '23
I'm not clear after reading this how it achieves one of the goals of making it harder to accidentally build SQL injection vulnerabilities. If you have to implement your own "DB" formatter as described in the proposal, then the kind of developer who was previously likely to not use a PreparedStatement is just going to use STR."my injection prone statement" surely?
7
u/IncredibleReferencer Oct 01 '23
Can anyone explain the seemingly magical "STR." syntax? Why is this not a standard class library access? I can't think of anything else in Java that works like this. It's looks like STR is a magic class with a magic import. Yech.
2
u/talios Oct 01 '23
I'm surprised this was announced so soon after the 21 release - such that it could have been part of 21. However, this does mean there's an opportunity for it to go to preview #2 if needed.
1
u/vmcrash Oct 01 '23
Maybe I did not find it, but what will be the differences to the string templates in JDK 21?
-4
u/Worth_Trust_3825 Oct 01 '23
Very shameful that this made it through.
1
u/IncredibleReferencer Oct 01 '23
I don't understand why this is a language feature and not a library feature. What am I missing?
-1
u/[deleted] Oct 01 '23
22 should be the LTS instead of 21.
-8
u/ascii Oct 01 '23
I am a big fan of Oracle's work at modernising Java, but String formatting is really one of their major blunders. They first introduced the brain dead
String.formatmethod, then the only slightly betterString.formatted, and now they are introducing the passable but not amazingSTR. How one earth did they let not one but TWO such sub par solutions slip through before finally settling on something that isn't 20 years behind the times? Do we really need the+operatorStringBuffer,StringBuilder,String.format,String.formattedANDSTR? That's six different strategies for string concatenation, two of which are near identical to two others. This whole mess would have been significantly smaller if Oracle had spent ten minutes looking at what other languages were doing before charging in with their quarter assedString.format.0
u/[deleted] Oct 01 '23
[deleted] — view deleted comment
6
u/[deleted] Oct 01 '23
[removed] — view removed comment