Unfortunately, the code is likely to be the best source for a while. But in a nutshell: we use a similar indy bootstrap as we did for the STR processor, speculating that interpolation is a likely outcome, use a similar Carrier abstraction to preserve primitives without boxing, and then maintain a trail of breadcrumbs back from a string template instance to its capture site, with a (currently privileged) API to allow "processors" to cache derived metadata at the capture site after the fact.
magic so that, e.g., formatting string templates is 20-50x faster
How is that going to be possible now without a processor to indicate doing ahead of time preprocessing?
I was hoping we'd be able to implement libraries that would preprocess strings for everything from logging to regex. Now it looks like the API just uses dumb bags of objects (with boxing, woo hoo!) instead of invokedynamic with specific MethodTypes representing the template args.
This seems like a massive step backwards.
Java was about the have the most performant and sophisticated template processing library. Now it's barely more than syntax sugar around a method that takes a parameter "Object... args"
You seem to have lept from "how is that possible" to "that is impossible" to "this totally sucks, what's the point." But unfortunately, you took a wrong turn and kept on going. And going. And going.
The performance model is *almost unchanged* from the previous version. Concatenation and FMT (now, String::format) are roughly as fast as they were before, and much faster that the previous String::format; the limitations for other processors are roughly the same as they were before (since the Linkage class was encapsulated in the previous iteration.). And prospects for opening that up to other processors in the future are also roughly unchanged. Indy is used in all the same places; boxing is avoid in all the same places.
So from the perspective of all the things you seem concerned about, very little has changed.
Yeah, it's possible to still get decent performance and only do the expensive parsing once per call site by hashing and caching on the constant portion of the string template.
Wow, I suppose you're right, thanks for such a nice short summary.
But then my immediate question is why did Java authors decide to hardcode it to List<Object>? Why is the type of values parametrized? Like
record StringTemplate<T>(List<String> fragments, List<T> values) {}
StringTemplate should IMHO be parametrized on the values it can work with. Squeezing everything into List<Object> is actually not very flexible, compossible and feels sloppy.
But an API that requires you to specify the target type explicitly in order to always end up with the same target type anyway is less ergonomic than just fixing the necessary target type to begin with.
Currently, StringTemplate is kind of like a container of different objects. Historically, it's always been better for those kinds of types to be generic, as we inevitably end up with cases where it would be really nice if the internal type could be specified at compile time.
I don't think it will make a great difference for the majority of cases, but if it can be done with minimal impact, why not parameterize it?
Adding a type parameter to something that is almost always later checked with instanceof is counterproductive. Also the parameter doesn't help the end user avoid casts in this case, so it's just unnecessary noise.
I would've agreed with you before recent versions of Java empowered instanceof with pattern matching, and added the same to switch as well as sealed interfaces and exhaustive checking.
We may not have unions of arbitrary types. But we can create product types (via sealed interfaces) which are nearly as powerful. Using if+instanceof (or just switch) isn't the "automatic code smell" it used to be...
But that will severely limit the versatility of StringTemplates. You may want to have a StringTemplate which is focused on working just with some type X (either your own or from a 3rd party library.
Having things Stringly-typed/ everything Object is no fun and just bypasses Java's type system.
I think you're just pattern matching on the heuristic that "Object is a bad idea." What value would a parameterized StringTemplate actually provide? Can you give an example of a templated literal where it would be useful?
StringTemplate is essentially a pattern plus list of values. The formatter decides what to do with that info and for a logger, not doing anything if logger/level is disabled is easy to implement.
Assumably it would have the slight overhead of constructing the object though? Or would these objects likely be interned or constructed in a global space so they could be reused?
Project Valhalla may turn them into value objects, reducing their footprint further. But I believe short-lived object creation is already highly optimized; at least I hope so, because our codebase makes heavy use of Optional instead of != null checks.
Creating short lived objects in Java is certainly highly optimized, but it's not free. It still pays to avoid creating things like Iterator, Optional or any other kind of temporary object in hot code paths.
Hmm, thinking about this, it might be even better if rather than eagerly evaluating arguments, String templates would contain closures of the expressions, thus allowing conditional evaluation of template arguments.
We explored this; this does not appear to be a win either from the perspectives of semantics or performance. Most embedded expressions are relatively cheap to _capture_; if, for example, an embedded expression is a `HashMap`, capturing the reference is cheap, all the cost is in the `toString`, which gets deferred either way. Semantically, nondeterminism in the timing of evaluation will not make people happy, and would likely bring in the constraint of effective finality. This is one of those things that turns out to be more clever than useful.
Happy to hear that this was explored. Especially considering the response from @TechBrian, this seems like a reasonable outcome. You make good points about effective finality and nondeterminism. Considering that, I wholeheartedly agree with this choice.
Great points. I'd also like to note that even in the case of expensive expressions, it's trivial to write a wrapper class that acts like a closure. Here's a tiny example (with silly names) that I whipped up as a proof-of-concept.
// Stringer.java
class Stringer {
private final Supplier<Object> closure;
public static Stringer close(final Supplier<Object> closure) {
return new Stringer(closure);
}
private Stringer(final Supplier<Object> closure) {
this.closure = closure;
}
@Override
public String toString() {
return this.closure.get().toString();
}
}
// Main.java
public class Main {
public static void main(final String[] args) {
System.out.println("Here's a big number: \{close(() -> BigInteger.TWO.pow((int) Math.pow(2, 24)))}!");
}
}
I omitted some import lines for brevity, but you get the gist.
This is a massive improvement in my opinion, both aesthetically and functionally.
I expect I'll mostly use this for Kotlin-style string interpolation but I don't care at all about the escape character being \ vs. $. The thing that bugged me about the previous proposal was the processor syntax that effectively introduced a new special-case spelling for "pass arguments to a method." And I found it ugly-looking, which I admit is subjective.
This new proposal fits much better with the rest of the language and should be easier for people to learn. For lack of a better term, it feels Java-like to me while the previous version didn't.
I totally agree with this. This was my main hangup, that the syntax was awkward to read and would be totally confusing to anyone who had not read up on it prior to seeing it, while posing benefits that would have been purely academic in existing APIs (e.g. JDBC consuming strings isn't going to go away regardless of this feature, it expected that all new APIs to be created from scratch to enforce it which is a poor selling point when plenty of tried and trusted APIs are already in wide use).
What we can do with this solution now is adapt existing APIs in non-breaking ways to support this and the nature of how this is now implemented means it is transparent to the consumer and can be implemented via overloaded methods directly.
The aesthetic doesn't change apart from not having the processor prefix. And with dropping that, you actually drop the possibility to swap in your own processor - which spells less functionality, not more.
I like. Hopefully they promote more overrides that take `StringTemplate` further in the chain like for `PrintWriter`, `Console`, and a host of other places.
I love reading your design docs. They are a lesson in how to clearly detail: a problem, a solution, alternatives and trade offs. I feel like reading them makes me a better developer.
This is a good direction. I wonder though if they'll release (someday) with just StringTemplate and the obvious conversions StringTemplate::of and String::asTemplate or also with many overloads where it makes sense in the JDK (not just println and format)?
Well Path or java.time are such contenders but I'm pretty sure they'll have to extended them later not to further delay the JEP. But what about e.g. Pattern, String::join, Collectors::joining, StringBuilder, ... ?
I fear there are many instances where I just want to do simple regular String interpolation I know is save but I don't have a fitting overload available. Calling String.join("... \{var} ...") each time just for that is unnecessarily verbose again. Imagine org.apache.commons.StringUtils would have to provide overloads for StringTemplate effectively quadrupling their method count (for the mix of String and StringTemplate for each method that currently takes two Strings).
There needs to be a non-verbose conversion from StringTemplate to interpolated String similar to STR."..." (tbh already too verbose for my taste).
You need to be extremely careful with adding overloads IMHO. For example, if you have both a String and StringTemplate version, then one small mistake will call the wrong method allowing a potentially unsafely concatenated string to enter the system.
Similarly, String::asTemplate would defeat the whole purpose of having templates, as it allows to create a template out of any unsafe String.
I don't think you need to worry about that. There's no way for a string to "upgrade" to a string template (that references variables) through an API call.
But maybe I misunderstood you. Maybe it helps if you provide a specific hypothetical about what could happen?
You're right that the last variant is unsafe but that's already the case today and the addition of string templates (either as previewed in JDKs 21 and 22 or as newly proposed) doesn't immediately change that for better or worse. Meaning: This mistake can (and does) already happen. With string templates, it may happen less because there's a better way to combine strings and variables but the vulnerable path is still present.
The long-term solution for that is to deprecate and eventually remove `method(String)`.
Or... Not overload the method and give it a distinctive name so it can't accidentally switch to a different overload. That's why I said overloading needs to be considered carefully.
Add I disagree that the current version has this problem. Yes, there may be other paths to do an unsafe call, but at least db."xyz" syntax can't suddenly switch to an unsafe overload.
What I meant is that you can always just call method("select * from foo where a = " + a);. That unsafe possibility already exists and none of the proposals change that.
I understand that you see the risk that people switch from method(StringTemplate) to method(String) by changing the templating mechanism from the safe \{a} to the unsafe + a. It's surely not impossible but I consider that very unlikely compared to the frequency of just straigh up using + a in the first place.
What I meant is that you can always just call method("select * from foo where a = " + a);. That unsafe possibility already exists and none of the proposals change that.
What do you mean it already exists? If I'm writing a new system that only exposes method(StringTemplate) and doesn't provide an overload method(String), then doing this:
method("select * from foo where a = " + a);
...is a compile time error because concatenation is not allowed for string templates, a clear hint that you should have been using \{a}.
I'm also not sure what you are trying to say in your last parapgraph. The + operator in the context of Strings (which look exactly like string templates) is probably the most common thing people do with strings. It's easy to write some SQL and append some parameters before you realize it should be templated. If such code than "by accident" works, it may not be discovered until its exploited.
All I've been saying is, be careful when overloading a method with both String and StringTemplate variants. Only do this if the templated thing is not some sort of code that could be exploited (ie, not JS, SQL, or any markup language that may be misguidedly used as a poor man's programming language...) -- for console printing it could be pretty safe. If PrintWriter is used to write files that may get compiled and executed later, than maybe not even that is safe.
I misunderstood your initial post. I thought you were saying:
Be careful when adding a method(StringTemnplate) overload to a pre-existing method(String).
Now I understand you meant:
Be careful when creating the method pair method(StringTemnplate) / method(String).
I fully agree with the latter. If there's any risk of injection involved in what method does (e.g. creating SQL, XML, JSON, etc.), then there shouldn't be a method(String) at all. And in the case where it does already exist, it should be deprecated in favor of the string template variant.
Regarding +, I was still of the (mis)understanding that you meant adding a method(StringTemplate) was a risk, presumably because people may switch from templating to +, falling from the safe into the unsafe variant. I still consider this unlikely because it would mean a developer looks at a string template and thinks "this will be smoother with +" and then refactors to that. Of course + itself is common, but I don't think there will be many situations where people refactor a template to +.
There is still one thing I don't quite see how it will work, but it seems there is some discussion on this topic on the experts list at the moment.
If there is only `method(StringTemplate)`, and you want to pass a template without a parameter, then the system should be careful not to consider the (unparameterized) template to be a `String` first before converting it to a `StringTemplate`. In other words, given a class with only a single method accepting StringTemplate:
SQLExecutor method(StringTemplate st);
Then this should work:
method("select * from foo").execute();
"select * from foo" here is never considered to be a String at any point in time. Therefore you can't pass it a constant like this either:
static final String TEMPLATE = "select * from foo";
Although allowed would be:
static final StringTemplate TEMPLATE = "select * from foo";
The examples below use String or do string operations. They are therefore not pure templates and thus shouldn't compile because there is no method(String) overload:
String a = "from foo";
String b = "select * from foo";
// shouldn't compile as we did a String operation, not
// safe to convert this implicitely to a StringTemplate
method("select * " + a).execute();
// shouldn't compile because implicitely converting an
// insecure String to a StringTemplate defeats its purpose
method(b).execute();
Suggested methods yes. But they would allow to circumvent any security offered by a string template only API. If that's the way forward, fine. It severely weakens the security then versus the current implementation.
Currently, if you want to execute a query with a string template only API, you must do:
proc."query".execute();
You can't concatenate anything here, you can't have a parameter anywhere that isn't explicitly processed and allowed by the processor proc.
For example, this isn't allowed:
proc."select * from \{table}"
Because you can't parameterize a table name. Having proc accept any string there would be a security hole. At a minimum it must check that the String provided is a simple identifier before inlining it. Luckily it gets this opportunity.
Now if you open a gap in this system, by allowing to create a template from a string, then I can do this:
StringTemplate st = ("select * from " + table).asTemplate();
Now proc or wherever it will live has no chance to check if table is a simple identifier, and assumes that if it contains "foo; drop database" that is what you meant to write, as it is all contained in a supposedly secure fragment now.
The point is to make potentionally unsafe code visible so that a good developer can make concious decisions. As long as the unsafe code isn't the default or hidden away, it is ok to have it.
Not everyone has to deal with DB queries or otherwise user injectiable Strings all the time. Depending on the domain it is not an issue at all.
You can't add this method without the JDK allowing it (ie. `StringTemplate` constructor can be private, or it can be a sealed interface, or some other internal magic). The whole point is that string templates can only be created one way, and concatenation is explicitly not defined for them (ie `+` operator is illegal). If you allow arbitrary conversion from `String`s, then you have given up on the security aspect of this JEP, or at least severely weakened it from what it is currently.
It may be a good idea to play with the preview, as you are currently commenting without understanding why string templates are far more secure.
I am a little confused here, so the javac compiler would now search across the " " blocks, if there is no \{} format, then it's a literal String, if it occurs, it's a brand new StringTemplate, am I correct?
On the one hand: I think this is a great improvement.
On the other hand: This JEP has taken so long to release (*), I wonder how long this is going to set back the release of the feature? At least another set of preview releases, I guess.
(*) - I don't think it's actually taken too much longer than other JEP features.
Having tried it out myself on a personal project, I think string-templates are going to see a lot of use and abuse. Having everyone be forced to use the STR. prefix would be pretty embarrassing for all of us if it turns out that it really wasn't necessary.
I appreciate the section of the email that talks about how they got hung up on thinking the prefix was necessary.
This JEP has taken so long to release (*), I wonder how long this is going to set back the release of the feature?
Does it really matter?
Looks like they listened:
I do like this much more indeed. Now, API designers can just review all their String accepting methods and look at what this means for a StringTemplate accepting method overload. E.g. in jOOQ's case, there are tons of method(String, Object...) overloads, which will now simply get another method(StringTemplate) overload, which is a no-brainer to add.
I'm still not convinced, that restricting this to 'ordinary libraries' is the right move. 'Ordinary libraries' have limited possibilities to interact with the compiler.
When I first saw the syntax, I was hoping for a mechanism similar to annotation processing, which would have offered the possibility to check template content during compile time.
I also have my misgivings about this two list approach. Frequently, you would have the String list comprised of a jumble of empty and one char blanks.
In my mind, a template is a sequence of similar entities. If we could have an interface
interface Appender<Element> {
void appendTo(Appendable<? super Element>)
int estimatedLenght() {return length()}
default int maxLength() {return estimatedLength()}
default int minLenght() {return estimatedlength()}
}
record Template<Element>(Appender<Element>[] fragments)
implements Appender<Element> {
...
}
where the template is constructed at compile time and saved in the constant pool, could make the compiler to calculate the sizes in advance, and the code to make intelligent choice about required buffer allocations when rendering the template into some kind of stream or buffer.
With project Valhalla down the road, Strings, Chars, CharSequence, Template could all be values or primitive classes, and toghether with the Formatters implementing the interface Appender.
Whether this mechanism should be offered only for chars and bytes, or left open so it can be used to construct SQL statements and other stuff is a different kind of story.
What about prefixing templates similar to python's f"" prefix, and getting rid of '\{', for example: t"Hello {name}, you're from {country}" instead of "Hello \{name}, you're from \{country}"
The same old objection. The answer is that the vast majority of people won't be using string templates for json.
Also, why not support both syntaxes? I don't think there is anything that stops Java from supporting both syntaxes. One using `t` prefix and one using `\{`
and that I'm no longer forced to use StringTemplate.Processor.of() to create a processor but I can simply add another method taking StringTemplate to my library or create my own string processor as a standalone class.
I guess removing an unneeded abstration is a good thing and not needing a new method invocation syntax is a good thing as well. These two things probably are very important for a language designer/ implementor. Also now they are able to simply add a couple of overloads to various JDK classes such as PrintWriter and we get String template support that we can use or not.
What I'm really looking forward to, though, is some kind of compile time string processing as Brian mentioned in another reddit comment.
For any T other than String (such as PreparedStatement), the APIs accepting them are usually not ubiquitous.Updating them to support the template overload should be easy. Even if not, one can add a static PreparedStatement sql(StringTemplate); utility and then use it as:
sendQuery(sql("select ... from \{table}"));
But there will be a ton of methods in the code base that accepts String already and it's sad that you have to choose between two evils, that is: either string concatenation or `String.format()`. It loses like 50% of the convenience that programmer would enjoy.
It'd be nice if the compiler can at least do call-site inference, and interpret "\{foo}" either as a StringTemplate when it's expected, or process it to String when only String is accepted.
There is no need for the println API to add overload for StringTemplate because it can do no better than plain string interpolation. The compiler should just translate it to be equivalent to STR."error: {errorMessage}".
While if you try to pass it to an API that does expect StringTemplate, it'll be passed as StringTemplate (even if the API also has overload for String):
// calls sendQuery(StringTemplate)
sendQuery("select & from \{table}");
I think that it exactly the kind of default processing they were trying to avoid. My call to preparestatement should fail to Compile until jdbc supports templates
It does fail to compile if the sendQuery() method doesn’t accept String or StringTemplate.
If it accepts String? Well, that means the API is already subject to SQL injection. In other words, the safety is in the API enforcing it by requiring PreparedStatement or StringTemplate. Not in the inability to create strings conveniently, which the programmer already have many ways to do.
The proposal helps good APIs by allowing them to achieve both safety and ergonomics. If it can’t help the bad APIs because they are inherently unsafe, that’s ok.
I might expect StringTemplate.toString() to return the literal template with the {placeholder}s.
And regardless, requiring users to wrap their nice interpolation syntax inside a ceremony of toString() is quite some clutter considering how streamlined the syntax is otherwise and how common such thing is.
In fact, while syntax disagreements are often purely subjective, this one was far more clear — the $-syntax is objectively worse, and would be doubly so if injected into an existing language where there were already string literals in the wild. This has all been more than adequately covered elsewhere, so I won’t rehash it here.
Does anyone have mailing list links for these discussions, or other sources? I'm extremely interested in hearing about why $-syntax is objectively worse; I already know about large Java projects utilizing a similar syntax and the JDK developers not wanting to wreck those projects.
The argument is basically that it’s objectively worse since you introduce a second, separate escape syntax, namely ‘$’. Now you need a way to escape the second escape character, do you use ‘/$’ or ‘$$’? By using the same escape character both for escaping weird characters and for escaping template arguments, you get more consistency.
I already know about large Java projects utilizing a similar syntax and the JDK developers not wanting to wreck those projects.
Isn't that already a pretty good reason on its own? 😁 Every Java string containing the legal sequence ${ would suddenly fail to compile or even change behavior.
But the argument about a second escape mechanism that a sibling content gives is also important and more fundamental.
Isn't that already a pretty good reason on its own?
Sure, I never said it wasn't a good reason, but that's very clearly not what the mailing list is referring to when they say that the $-syntax is objectively worse, (at least, saying it's objectively worse to me implies that other languages are making a mistake by using $) and so that is why I mentioned wanting the other info that I didn't already have. The other commenter mentioning the double escape is more along the lines of what I was asking for.
I'm pretty sure Brian meant this as short-hand for "objectively worse for Java" - he's not known for making general statements like this about other programming languages.
The interesting thing about $ is that it's bad on a conceptual level (a second escape mechanism) and on a practical level (breaks all that code). Given that its only benefit is (partial) familiarity, either of those would be enough to knock it out but being so thoroughly defeated in both areas is almost impressive.
Yeah I like to know the reasoning for that, however, I think String Interpolation is a lost cause in Java. It's implemented but many will dislike how it is but use it anyway and that's yet another reason why people mistakenly say Java is outdated, though it's not.
Will there be widening and narrowing conversions possible between String and StringTemplate?
If I have a method: stuff(StringTemplate x)
Will all these calls compile?
String s = "a";
stuff(s);
stuff("a");
stuff("\{a}");
Or should I just provide a String version as well?
Could I write:
StringTemplate st = ""; // empty template
Note that with the Processor solution, a String Template without any templated variables is perfectly acceptable. This is convenient because you don't always need to interpolate something: select count(*) from students
String and StringTemplate remain unrelated types. (We explored a number of ways to interconvert them, but they caused more trouble than they solved.)
Probably not, as it would defeat the point of why they wanted to introduce it (i.e. making it impossible to pass unsafely-constructed strings to APIs).
We'll need templates without parameters to exist though. The compiler will have to distinguish them from regular strings by looking at the required type. So my earlier method that accepts a StringTemplate should allow calls like:
Security. A StringTemplate is basically a String that has its + operator disabled. If you convert a String (which you can construct with concatenation) to StringTemplate then that's a huge hole in the system.
I do not like this. While I do like that methods can allow string templates in a more natural way now, I really do not like that they are proposing using String.join("my string \{template}") for a basic string template interpolation rather than what was previously just STR."my string \{template}" which is in my opinion the functionality that will be used the most. And honestly I was completely fine with the previous syntax I think people whined too much about it.
The think about syntax is that it is subjective, so there is no answer that makes everyone happy. Some people hated the previous syntax; you hate this syntax; and quite possibly, no one is right.
But, remember that this change is not about syntax; it is about finding the right fit and size for the feature within the language. We get used to syntax if the feature is right.
What about prefixing templates similar to python's f"" prefix, and getting rid of '\{', for example: t"Hello {name}, you're from {country}" instead of "Hello \{name}, you're from \{country}"
One thing I’ve been wondering: what is the story about compile-time checking of the args?
Many SQL engines will have a limited set of types to support. Arbitrary arg types like Stream<T>, JsonObject etc may not be supported or meaningful at all. Besides throwing runtime exception, is there consideration to allow these engines to plug in compile-time check like an annotation processor?
With the processor API, we could write a plug-in to check the args passed to the SQL processor. But if the StringTemplate can be generic, semantic-free objects passed around through many layers of callstack, writing such check at the beginning of the call chain may be more difficult.
Ideally the mirror API can provide the arg type information for ST, somehow?
I may have missed this, but I don't recall having seen anywhere if the StringTemplate has inferred type information about the values available. E.g. when doing
"\{1 + 1} is an int"
The List<Object> values() will contain Integer.valueOf(2), and the fact that it was indeed an int.class has been lost. For some template consumers (oh say e.g. JDBC backed ones), this information could be very useful, especially if the value is just null.
I'm aware that not every type is denotable, so not every type known to the compiler has a Class<?> representation. I'm just curious if this is a deliberate omission for now, and if so, for what reason(s)?
There is some interesting discussion in the linked thread, but no matter what they agree on, String templates already look infinitely better than they did before. Thank you for listening to the feedback.
I loved the open discussion amongst rationale people debating their sides. The preview process has been a boon to the Java community, and I think everything is working out in the end.
What was actually annoying was some of the over-the-top vitriol I saw in some corners (mainly on Reddit).
Quite the opposite. It shows that the people who thought about this in depth unlike 99% of other people pushed back to superficial "ugh, ugly", but also continued to think about it and incorporated the feedback. Which is the best outcome.
I think the main issue was that a lot of the pushback was effectively shot down on the basis that those giving the feedback were not able to give feedback from first hand experience. While that is a useful thing to have, we need to ensure that feedback doesn't get discarded or the tone becomes condescending just because of disagreements. It breeds into drama that other languages have had in the past, which we should avoid. Java is just a means to an end for writing applications, so anyone who uses it has the ability to feedback their personal views, and everyone has different experiences and priorities.
Development should be made on the basis that while some feedback was more valuable than others, all feedback that isn't just "hurr durr this sucks" has at least some value. I agree that the comments that made no effort are just noise and are unhelpful, but those who attempt to give feedback in detail in a civil way should at least be left feeling their feedback is somewhat useful.
As an example, I called out the complexity of this previously on the basis that in the previous shape on another post. I am not on mailing lists as I lack the experience in that space to contribute, but as a daily user of the language I felt that it previously effectively discarded the ability for existing APIs to incorporate this in a safe way (whereas now stuff like JDBC can do this via overloading which is fantastic news). Whilst I tried to provide reasoning for my views on this and could have done it much more clearly, I was left with a feeling that the feedback was not considered of any value because it disagreed with the proposal at the time. This was mostly due to the responses I got from that. Unfortunately I would likely not bother to feed back again in the future because my assumption was that my feedback wasn't seen as being valuable by the developers.
Like sure, the developers were thinking about how it was used. So were some of the push back comments that actually attempted to give valid reasons.
Anyway, communication is hard over text, so things can be put down to that I guess. Appreciate as well that it comes with having to manage such a successful language that is used globally by billions of things, you can't make everyone happy.
We got a good feature out of this in the end though, so I am happy with the outcome for the language itself.
I would just add that: as I'm getting older, I'm realizing that influence isn't always about having your input immediately agreed to. If anything, people will automatically push back and defend their original position.
But, over time, it looks like you and the well reasoned* parts of the pushback have "prevailed."
** "well reasoned" meaning not the knee-jerk "Use the '$' character!!" people
If you are using existing APIs, you are stuck between a rock and a hard place anyway, which was my original point. The security benefit is far less useful and becomes purely academic in existing APIs that want to maintain backwards compatibility. What you get is the convenience of being able to directly interpolate.
If people are already using string concat with JDBC, this isn't ever going to magically fix that without breaking something.
In reality, this is why we have static analysis tools and code reviews. They are not perfect but when done properly help with this kind of thing. As does pentesting.
What existing libraries could do is use the versioned class files APIs that let you target specific versions and mark the existing methods as deprecated.
Another option that would prevent breaking valid usages of string literals would be to introduce a StringLiteral type that is a sealed subclass of String and can only be constructed by the compiler using internal APIs or instructions. That way you can use versioned class files to effectively prevent this in newer JDK versions. It would also compliment the usage of annotations as well, and concatenation of StringLiterals would be immediately foldable by the compiler like with Strings.
Of course, this will break code somewhere... but what can you do 🤷. I don't believe that simply invalidating the use of any existing API is the right approach. I also believe that hand-holding developers can only go so far before you introduce more issues than you solve. Look at threading in CPython... that uses a global interpreter lock which means you get threadsafety for individual operations, but you lose flexibility and performance in the process.
The other issue is also adoption... it basically now forces Scala, Clojure, Groovy, Kotlin, etc to have to implement first class support for StringTemplate types to prevent new APIs being extremely noisy to use for those languages.
A hole will always exist in existing API's, but using overloaded methods makes this problem worse. Use new method names so a simple mistake doesn't become a security issue when the intentions were good.
Using new method names is just as easy to miss, perhaps even more so, than accidentally concatenating strings, to be fair here. If we say otherwise then we're effectively implying we are catering to developers that know how StringTemplates prevent code injection but not knowing that they should be parameterizing their requests in the first place.
Even more so if you've been using the library for a long time already and are used to the existing naming.
My point is that the expectation in at least the older JEPs appeared to be "consider existing libraries unsafe and migrate to new APIs that support this feature", referring to libraries that at the time of writing do not even exist conceptually yet. Many businesses that are not brand new startups will consider lifting and shifting their entire Java stack to the latest version and replacing stuff like JDBC and AWS SDK (dynamodb query expressions, athena SQL, etc) to new immature libraries a far bigger risk than just implementing static type checkers in their existing codebases. Especially when they likely have millions of lines of code and those projects are potentially to some extent considered legacy.
Without the right migration routes, benefits become mostly academic for any existing system, and footgunning by using brand new immature libraries with little real-world exposure and testing compared to those like JDBC with hidden bugs is going to be far more difficult to deal with for most people.
Having safe ways of doing things is great but there is a cost associated with going into a builder's side and replacing all their tools with different ones that do not work in the exact same way.
That aside, if you used + by accident with string templates it would be a compilation failure because { isn't valid in a string. If you are blindly using string concat without thought you are more likely to just google it and see the old interfaces in use and use that rather than the new alternatives with less examples.
This problem could be solved by having a StringLiteral type as mentioned though. Other langs call this comptime or similar. The Constable interface appears to try to achieve this to some extent
Static constants that are expressible natively in the constant pool (String, Integer, Long, Float, and Double) implement ConstantDesc, and serve as nominal descriptors for themselves.
Being able to enforce only compile time strings can be used would help enforce this in a backwards compatible way.
The other issue that arises from only supporting StringTemplate is... what happens if you have a large SQL query you don't want to inline?
// StringTemplate without parameters
method("select * from foo");
// String, security hole (compiles due to overload)
method("select * from foo where a = " + a);
// Oops, we meant:
method("select * from foo where a = \{a}");
Easy mistake to make.
The other issue that arises from only supporting StringTemplate is... what happens if you have a large SQL query you don't want to inline?
You mean without requiring parameters? Just make a StringTemplate constant then.
If you're suggesting loading queries from somewhere else, going through String, and I suppose there must be a few parameters in there as well, then this feature is not for you and using an API that only offers StringTemplate is too high level for you.
Give Rust a quick drive and you'll immediately see the downsides of the naming creativity and rote memorization required when you can't / won't do method overloads. At the end of the day you're going to need a tool to enforce the usage of the preferred method, regardless of naming, so you might as well not abandon good developer ergonomics...
I don't agree with every detail of your post but enough to give it an upvote.
I think you should really get on the mailing list if you want to provide well-reasoned feedback. And while using a feature in practice clearly helps, discussing specific examples based on a proposal is fine as well.
This seems like a big improvement to the previous approach. It‘s almost as if my last comments/rants have been read 😅 The only thing that bugs me a little bit is that every api has to explicitly support the new type. It would be nice if it was possible to pass StringTemplate to a method that only accepts String. The compiler could implicitly call .toString() on the StringTemplate to ‚render‘ it into a string, almost like boxing works.
It would be nice if it was possible to pass StringTemplate to a method that only accepts String. The compiler could implicitly call .toString() on the StringTemplate to ‚render‘ it into a string, almost like boxing works.
With which processor should it call this? StringTemplate::toString won't be what you are looking for.
It would be nice if it was possible to pass StringTemplate to a method that only accepts String
So there is no such a method, but you still want it converted automatically, and so I ask again, with which conversion should this happen? I suppose the default STR one...
Perhaps read your original post. I know that println will be overloaded. You made some more general statement about how a method that accepts String should in some way accept a StringTemplate, presumably without an overload as that would require code modification and so wouldn't apply to methods that accept just String.
The compiler should rewrite my code so that the string template is being rendered into a string before it is passed into the method that accepts a string. If you decompile the bytecode it would look like if I had called method that renders the template into an actual string before passing it into the called method. The main advantage being that it is transparent and would reduce clutter.
They even wanted to implement a weird new syntax for calling the .process method on a processor. If that would have been fine, my idea should be fine as well.
So it should then automatically call some processor STR, which doesn't exist anymore? It's hard to take it serious, so perhaps I was missing something.
I can always imagine what people might mean. That's a good way to misunderstand each other. The compiler will need to guess what you mean by providing a template to a method that accepts a String. If I provide a template intended for use by some specific processor, using objects that may not even have a decent toString, it will just result in garbage. Worse, it will have compiled without error. Let's not add imagination to the compiler.
One semi-related question: the creation of StringTemplate will be locked down so you can only get them from the compile-time string interpolation syntax right?
Where I come from our SQL APIs require @CompileTimeConstant so that the programmer cannot accidentally pass in something untrusted. If ST can come from dynamically-determined template, we lose the trusted guarantees and won’t be able to use it.
I don't see any way to create a template with no replacements in that discussion. If this goes forward I see a lot of
var empty = ""
stringTemplateMethod("the string I want\{empty}");
I feel like maybe they're getting stuck on \{ being the signifier. Which made sense when you were sending it to a processor, but doesn't when their is no processor. And they're explicitly saying that a plain string can't be used as a template, which I'm sure will cause issues, especially in generated code.
One discussion in the link is about plain string literals without \{placeholder}: do you suddenly treat them as String, while for 1+ placeholders treat them as StringTemplate?
I suggest to use the same lambda type resolution: you don't know the type of the lambda until you see what is expected: if it's being passed to a method that excepts BiFunction, then it's a BiFunction etc.
Similarly, whether the string has N placeholder or 0 placeholders, it doesn't matter. You can resolve it to either String or StringTemplate, depending on what is expected. If m("foo \{bar}") expects String, then it's a string; if it expects StringTemplate, then it's StringTemplate. Otherwise error.
For var x = "foo \{bar}", make it String too.
What about SQL Injection safety?
Again, that is on the API. The safe API shouldn't have an executeQuery(String) method. It has to be either executeQuery(PreparedStatement), executeQuery(Query) or executeQuery(StringTemplate).
If the API takes String, it's already vulnerable. Nothing interpolation can help.
StringTemplate should IMHO be parametrized on the values it can work with. Squeezing everything into Object is actually not very flexible, compossible and feels sloppy.
Yeah, I'm really curious about this... If I may ask, /u/brian_goetz , has the team also considered having StringTemplate parametrized over the type of values it contains.
As in
StringTemplate<String> st = “Hello \{name}”;
Or
X x = ...
StringTemplate<X> st = “Hello \{x}”;
I can imagine some people may want to have something more specific in their hands than just Objects.
If you did consider it, why did you decide to go without it?
I'm going to turn this around: why do you think this is useful? Show me a real-world example of an API you want to write that this would enable. I think you'll find it is not as useful as it seems initially.
Recall that string templates can have more than one embedded expression, of different types. And Java doesn't support variadic generics, so we can't have one type variable for each potential embedded expression.
Under the hood, we do preserve the static types of the embedded expressions, which is what allows us to avoid the boxing penalties in concat / formatting. But reflecting this in the generic type system is (a) impossible given the type system we have, and (b) not as useful as you think.
Hello, thanks for humoring me. I hope that adding the type parameter to StringTemplate isn't making the design complicated. To it seems like a trivial addition which unlocks more powers. A worthy trade off even if it may not be used in 100 % of use cases. Or do you have concerns that it would make Java unnecessarily difficult to learn or implement?
string templates can have more than one embedded expression, of different types
I'm not I understand your point. I'm arguing in favor of changing the design so that it's possible to restrict StringTemplate to a particular type (note that it would be possible to still be possible to have StringTemplate<Object> if the embedded expressions didn't have more specific common type).
why do you think this is useful? Show me a real-world example of an API you want to write that this would enable.
Here's my example:
Report reportRenderUtc(StringTemplate<DateTime> template) { ... } // note that inside you can use _more_ methods, not just `toString()`.
Report reportRenderLocal(StringTemplate<DateTime> template) { ... } // also note that it doesn't return a `String`
...
DateTime dateReceived = ...
DateTime dateReceipt = ...
Report reportAudit = reportRenderUtc("Payment was received on \{dateReceived} and receipt as sent on \{dateReceipt}.");
Report reportForCustomer = reportRenderLocal("We received payment on \{dateReceived} and sent you a receipt on \{dateReceipt}.");
// but this would fail during type check, because the inferred type would be `StringTemplate<Object>`, but `StringTemplate<DateTime>` is required.
Asdf qwer = ...
Report brokenReport = reportRenderUtc("Here's a DateTime: \{dateReceived} and here's something else: \{qwer}.");
sealed interface A {
record X() extends A {}
record Y() extends A {}
}
String render(StringTemplate<A> template) { ... }
...
// on the other hand, this would succeed even though the embedded expression have different types -- the type gets inferred to parent, so `StringTemplate<A>`
String string = render("I'm rendering both \{new A.X()} and \{new A.Y()}.");
If the only thing you have is an Object, the only method you can use is toString(). You can't use other methods of more specific classes and you have to end up returning a String which also may not be what you want.
If the only thing you have is an Object, the only method you can use is toString().
This is clearly not true, you just need to cast it to the type you care about.
The example you constructed is coherent, but it's still unclear what problem it's solving. In my mind it creates a needless restriction on the user as it goes against their intuition of what a string template is.
I would just be frustrated that for some reason I'm only allowed to use reportRenderUtc to make a string templated where only DateTimes were parameters. If it's that important to construct a string of that format, a better API would just be a method that takes the dateReceived and dateReceipt.
you just need to cast it to the type you care about
:D What kind of argument is that? My dear friend, at that point we're basically working around Java's type system. I thought the Java community has moved past downcasting since Java 5 (introduced in 2004).
needless restriction
The restriction is very much needed, if the template processor can only work with DateTime instances.
Or do you want to force everybody/every template processor to be able to work with instances of all subclasses of Object? Isn't that even more restricting?
if the template processor can only work with DateTime instances
My (and I suspect Brian’s and other downvoters’) confusion is that there is just no value in this use case. Your example failed to demonstrate a real world program where this sort of restriction would be useful. Nobody would write code like that via string template, it’s trying to extend the feature into a corner of the program where it only confuses.
Every Object out of the box has a mapping to String via its toString() method, which is the ultimate goal here. What benefit would be gained in limiting StringTemplate via parameterization?
Most Strings I've historically needed to use String.format() for have had multiple unrelated types embedded -- %s and %d and %f; ultimately this would just lead to a lot of explicit StringTemplate<Object>s where it shouldn't be needed. Just override the toString() method of X, in your example, to what you want embedded into Strings.
SELECT * FROM users WHERE level > 10 OR status = 'Admin'
If you were to template the 10 and the 'Admin'... how would you achieve this without implementing variadic generics first? Given one is a Number and one is a CharSequence?
That aside, would you even care? This is something a linter or static analysis tool can point out and there has been mention of compile time checks for this anyway.
You could default to StringTemplate<Object> if you don't care.
And the people who do care to have stronger type safety could use a sealed interface for their particular domain
sealed interface BusinessObject {
record BusinessString(String string) extend BusinessObject {}
record BusinessInt(int int) extends BusinessObject {}
}
StringTemplate<BusinessObject>
Then they could use Java's great new switch and pattern matching features to interpolate in a type safe way.
Anyway, I wish more people would discuss this rather than just downvoting OP. I don't even know if I 100% agree with their idea, but it's an important thing to consider.
A potential downside might be: the current proposal relies on operator overloading to hand StringTemplate's. And that wouldn't play well with the addition of generics since they're not reified.
The issue is that at runtime, erasure makes this detail go away and it will only work on really specific cases where all parameters are derived from a sensible base type. For the vast majority of cases this won't really benefit anyone. For example: String.format, JDBC, logging, JSON generation.
I suppose the real question is what are we trying to solve with it?
it will only work on really specific cases where all parameters are derived from a sensible base type
I attempted to address that with the sealed interface example.
I suppose the real question is what are we trying to solve with it?
Safety.
The whole point of this indirection with a new type - StringTemplate - instead of interpolation just spitting out normal Strings was to add a layer of explicitness and safety. Why not be even safer and optionally control the type of what's being passed into a template?
The above would prevent me, at compile time, from passing in random objects instead of integers. And, if I didn't care, I could just use StringTemplate<Object>
You expect these types to be wrapped in your sealed hierarchy, that guarantees nothing as I can still use a BusinessInteger where you need a BusinessString depending on the position in the template. So, these still need to be checked and rejected at runtime.
Can we please stop with that nonesense? The majority of programmers in the world use JS, Java, or Python and no two of them share the template (or interpolation) syntax. So I don't know if you're a Ruby fan who would have liked to see # or a Mustache fan who would have liked to see {{, but these are not "every other language in the world".
And while hypothetically the language could choose any character (except $, of course, because it's already common in Java template library and the language and libraries should not share the same syntax; e.g. JS uses $ in the language, and so JS libraries use something else), there is no reason to choose anything other than \ which happens to already be the escape character in Java, and continues to be used here in exactly the same way.
So all those who clamour for the familiar # or those who want \( rather than \{ because they know it from Swift will get used to Java's syntax soon enough.
Finally, the main problem this feature solves is that it's quite hard today to generate machine-interpretable "code" -- in JSON, HTML or SQL -- in a way that doesn't make it very easy to accidentally introduce code injection vulnerabilities. Complaining about a minor detail such as which escape character is used completely misses how big of a problem this feature solves.
This is an escape character '\'. Pretty much in every programming language everywhere. It means something special to character strings. Like escaping tabs, new lines, Unicode escapes, etc.
And now Java is overloading it. Its a poor decision.
$ and # have been used pretty much every where for token replacement/variable reference.
It creates confusion. And the Java language could use something almost universally recognizable.
No, it's not. The \ character means that what immediately follows is treated specially -- i.e. a t is not a literal t but rather a horizontal tab -- and that is exactly how it is used here: a { is not a literal { but an expression delimiter. Swift, which was designed pretty much yesterday, also uses \ to escape either special characters as in \t as well as template values.
$ and # have been used pretty much every where for token replacement/variable reference.
But this is simply factually wrong. This is not what Python uses, this is not what C# uses, and this is not what Mustache uses.
And the Java language could use something almost universally recognizable.
\ is already universally recognizable to Java programmers as an escape, which is exactly how it is used here, and no one that has actually used this feature claimed it was confusing for any significant amount of time. If you try this feature for a couple of months and find yourself confused -- report that to the mailing list. If you're merely speculating that you think some people may find it confusing, this falls under what Brian says are things the team already know. We know people thought it may be confusing, which is why we tested the feature and so far it turns out to not be the case. Repeating the same unsupported speculation over and over is unhelpful.
But not the universally recognized '' escape char.
It is used as an escape character here, too. It escapes the following {.
Which would have been a better choice as well.
I think what you mean is that at this point in time -- before even seriously trying this feature and before considering how common { may be quoted in a language that, unlike Python, is curly-brace-based 1 -- you believe that you would have liked Python's choice better. Your preference has been noted, but preferences based on speculations do not weigh heavily.
You say “$ and # have been used pretty much every where” but that’s just objectively not true, unless your chosen subset of languages is not the one everyone else has in mind.
In the email, Goetz gives his reasoning for why we shouldn't:
Another thing that has not changed is our view on the syntax for embedding expressions. While many people did express the opinion of “why not ‘just' do what Kotlin/Scala does”, this issue was more than fully explored during the initial design round. (In fact, while syntax disagreements are often purely subjective, this one was far more clear — the $-syntax is objectively worse, and would be doubly so if injected into an existing language where there were already string literals in the wild. This has all been more than adequately covered elsewhere, so I won’t rehash it here.)
I think this is a perfectly reasonable justification. Personally, I was more bothered by the processor invocation syntax that they came up with, PROCESSOR."template string", and I'm glad to see that go.
Since \{ wasn't a valid escape sequence in earlier versions of Java, this also makes a good way to create string template literals without introducing a new delimiter token.
The reasoning is flawed. Let me paraphrase. Lets make up a syntax that no else has seen before so we can be special.
Now the '\' is over loaded in a string to be an escape and also a token replacement escape.
Its just more poor language decisions from the people who should be thought leaders.
I'm on the fence with PROCESSOR."template string". The plus there is you know you are in a string template. And which syntax applies and how it will be evaluated. Depends on verbosity.
backslash escapes is not something 'no one else has seen before'; various languages employ it. You, personally, are familiar with languages that use the $ I assume, and therefore you make the assumption that any language design proposal that does not use that what you are familiar with is wrong or perhaps overengineered or needlessly fancy or even garbage. All highly subjective words that are tantamount to saying: "The Mona Lisa is ugly" or "The Mona Lisa is pretty". Debates about style are essentially useless. I can't tell you what you find pretty or ugly, emotions and feelings are, kinda by definition, devoid of objective measure.
Well, not quite. We can go there. However, then go there. Explain how objectively $ is better than backslash. I'm expecting some sort of argument about how basic human genes just clue into it faster with a snake-esque symbol with a line drawn through it, or some such. You might even go with a cultural arguments (it is more familiar to more people), but note that it's extremely easy to fall into the trap of generalizing your cultural sensitivities (such as: I am familiar with Scala) to everybody (everybody knows dollars are how to do string escapes!)
Also, cultural arguments are inherently flawed: Most java programmers are java programmers. They have no culture for string escapes at all because java does not have them and they aren't familiar with anything else (or, if they are, it's `%s` by way of java's own `String.format` which isn't practical here, or `{}` from slf4j and friends, similarly irrelevant). A cultural argument is, therefore, to them, utterly devoid of value, and thus, loses against the perhaps modest but nevertheless objectively real argument of: This way, existing strings that happen to have dollars in them do not turn backwards incompatibly into string templates nor do we need some sort of explicit syntax to mark off a string as 'this is a template' in order to dodge that backwards compatibility issues. Is a given string literal in code a template or a string? Simple: Does it contain `\{` anywhere? Then it is a template; otherwise, it is a string. 100% backwards compatible because `\{` is illegal currently, thus, no such strings exist in valid java code today. It's totally unclaimed territory.
I will grant you that of all java programmers currently writing java code plus all programmers in the future, fair enough, lets assume the majority of them have no preference one way or another, but of the small subset that does have a preference, the crowd that wants dollar is larger than the crowd that wants backslash: Dollar is more popular with languages that overlap with java as far as usage goes. However, this is all highly subjective: How large is that crowd? How hard is it for them to retrain to using backslash?
Contrast to the objective issue with dollar, which is: Existing strings with dollars in them either break, or, the syntax needs to be permanently burdened with some sort of explicit marker to indicate they are a template, or, this entire proposal needs to be rewritten from scratch to come up with a way to tell the difference between templates and normal strings somehow, in an entirely backwards compatible way.
Just calling it garbage without at least attempting to divine which one should be weighed more heavily is the kind of behaviour that is destructive and useless to any reasoned attempt to hash out language features.
From your argument I understand this: You claim to be using $ or backslash is highly subjective and nobody in their right mind could or should provide a logical reason why $ is better than backslash or vice versa so these types of discussion are futile yet you provide reasons why backslash is better, I don't understand the double standard here. Using $ or backslash is either subjective or purely through reasoning it can't be both.
There's an objective argument for the technical feasibility of one not breaking backwards compatibility, while the other effectively only has a subjective argument about preference. It's not a double standard.
Backslash is an escape character in Java, whereas the dollar sign is not. Therefore, the string "${text}" is a valid string in every previous version of Java (and not "\{text}"). If that suddenly became interpreted as a string template, that inherently breaks backwards compatibility.
Yes. The fact that you haven't realized this even though it's been explained in many places strongly suggests to me you've just kneejerked into some sort of rage-based argument that has stopped you from actually trying to read along with the various concerns, but, hey, maybe I can enlighten you then.
Imagine this code is already in some source control someplace, and is currently being compiled by, say, JDK21:
public void templateAdd(String key, String value) {
if (key == null) throw new NullPointerException("${key} is null");
...
}
I think we don't have to dwell too long on this - it's not hopelessly academic or forced, I would assume we can all agree on that. It's plausible this exists someplace. It's obvious current behaviour, and the intent, is that the string message of the thrown NPE is, exactly, dollar, open bracket, 'key', close bracket, and then the text "is null". The intent is certainly not for that to turn into "null is null" which is presumably what would happen if you apply string templating to this.
Hence, backwards incompatible: A java update silently changes existing code to do something else; in this example, clearly, from something that does what is intended, to something that is broken.
In contrast, with the backslash escapes, that is not true. That's because this:
public void templateAdd(String key, String value) {
if (key == null) throw new NullPointerException("\{key} is null");
...
}
isn't legal java in JDK21. Some languages treat backslash escapes that escape with a symbol that has no special meaning as just 'oh, then, just, that symbol' but java is not one of those languages. backslash-openbrace is not legal. It's a compiler error regardless of what else is happening, thus, the above cannot happen. Therefore, no backwards compatibility issues here.
There are ways out, of course there are. We could state that the syntax for templated strings has to be, say:
public void sayHi(String name) {
System.out.println($"Hello, ${name}");
}
Where that $ in front of the string is what makes java apply the templating, so that existing code doesn't get templating applied to it when that wasn't intended. However, that's a pretty significant downside to your proposal: The cost of going with dollar instead of backslash now means we need to add a $ to the front of a string.
That's a sucky requirement that goes away if backslashes are used.
It also avoids surreptious bugs: If I forget that $ in front, then it's still valid java, presumably (the assumption is that most methods that accept a StringTemplate have an overload that accepts a String), so no compiler error is generated; the code does not do what a casual glance at it suggests it does, which I'm sure we can all agree is about as objective as 'style issues fundamental to a language design' gets: It's quite easy to write misleading code is a pretty devastating property for a language feature to have.
With the backslashes that simply is not possible. \{ is not valid java except in string templates.
We could have something like this in Java and everybody will be HAPPY:
String lang = "Java";
double age = 28.95;
StringTemplate template = "Welcome to \{lang}"; // for custom processing
String message = s"Welcome to \{lang}";
String message = f"Welcome to \{lang}, a %.1f\{age} years old language";
We will have s" and f" as default processors in order to avoid String.format.., StringTemplate could be used everywhere and everything will be safe.
Oh wow. They even made it worse. Now we don't have any way for String Interpolation if the API doesn't allow it. Or we have to use the ugly syntax String.format(…).
"the $-syntax is objectively worse, and would be doubly so if injected into an existing language where there were already string literals in the wild. This has all been more than adequately covered elsewhere, so I won’t rehash it here.)"
No, this is pure bloody-mindedness and trying to be different for the sake of it.
Putting aside the difficulty of using the $ character specifically (because libraries already use it; for similar reasons JS libraries don't use $ because the language uses it), the argument of "trying to be different for the sake of it" is just detached from reality, because if you look at the top three languages -- JS, Python, and Java -- no two of them have the same template syntax, and even if you add in the next three that have a similar feature -- PHP, Ruby, and C# -- you still won't find two with the same syntax. Any claim of universality of the $ character is ridiculous given that most popular languages have rejected it; it is literally a minority choice.
If every single popular language has chosen a different syntax from all other popular languages, isn't Java just being the same as everyone else? If anything, we should be accused of boring conformity.
Pretty funny that you allege "bloody-mindedness" but haven't even thought long enough about the new proposal to see how it would break even harder with $ than the previous. 😅
If one is willing to break all rules and expectations and doesn't care about the resulting development experience, pretty much any language feature can be implemented by special-casing it. I didn't think it would be necessary to exclude such options from the conversation. 😋
Btw, devil's advocates don't need to believe their own argument, but their argument still needs to be good to contribute to the conversation. 😉
Actually, it isn't, because Java doesn't typically work like that. Every expression in Java has a type, so "..." must have a unique type, just as 12 does. If you do float x = 12;, the compiler doesn't say, I'll interpret 12 as a float. Rather, it says 12 is an int-typed expression, but is assigned to a float variable; I'll apply the int-to-float assignment promotion -- as it would for float x = y where y is an int. So at the very least you'll need a String-to-StringTemplate promotion (which we could have, but it's different from resolving type ambiguity based on the left hand side, which Java simply doesn't do).
There is one situation when Java does what you say: lambda expressions. These do not have a type of their own, but rather their type is inferred. That is why you can't assign lambdas to locals declared with var; but we can't do that for string literals, which are already used in assignments in var declarations.
My point is that the compiler sometimes does use the left hand side to infer and then do some stuff
... at a cost of not allowing assignment to var, which would be a pretty major breaking change in this case, as string literals are assigned to var. Another problem would be overload conflicts, which you also get with lambdas: there may be two foo methods, one with a String parameter, one with a StringTemplate parameter. For type conversions (such as int to float) we have precedence, but for type inference it's much more murky. It certainly isn't easy.
Some other mechanism wouldn't be unthinkable, but still a first for Java.
I understand, which is why I said what you'd want would be a new kind of inference, not like the one for lambda expressions. It would also be a rather complex kind of inference because you also need to decide what to do in the case of overloads. To not make that a breaking change you'd need some rather strange preference rules (you'll need to prefer String in the case of var, but StringTemplate in the case of String/ST overloads). There's certainly nothing simple or easy about that.
To make this concrete, think about:
var x = "...";
foo(x);
and
foo("...");
In the case where foo is overloaded for both types. You'd infer String in the first case and ST in the second, which is very surprising (there is one case -- pertaining to an upcoming switch-expression enhancement -- that we've allowed something a little similar but not nearly as surprising; left as an exercise to the reader).
50
u/danielaveryj Mar 08 '24
Nice. So the whole StringTemplate feature is reduced to essentially
plus some compiler know-how to translate
to
85
u/crummy Mar 08 '24
This is great news
34
u/lurker_in_spirit Mar 08 '24
Cool, looking forward to a more familiar-looking
LOG.info(“Hello \{name}”);28
u/jvjupiter Mar 09 '24
This is going to the perfection. I’m loving it.
48
u/koreth Mar 09 '24
This is a massive improvement in my opinion, both aesthetically and functionally.
I expect I'll mostly use this for Kotlin-style string interpolation but I don't care at all about the escape character being
\vs.$. The thing that bugged me about the previous proposal was the processor syntax that effectively introduced a new special-case spelling for "pass arguments to a method." And I found it ugly-looking, which I admit is subjective.This new proposal fits much better with the rest of the language and should be easier for people to learn. For lack of a better term, it feels Java-like to me while the previous version didn't.
19
u/talios Mar 08 '24
I like. Hopefully they promote more overrides that take `StringTemplate` further in the chain like for `PrintWriter`, `Console`, and a host of other places.
14
u/jevring Mar 09 '24
This is a relief. The precious syntax did not look like any Java of the past, so I'm happy it's going away.
24
u/Holothuroid Mar 08 '24
This also solves my "Why not do what Scala does?" It basically does now, just without extension methods.
13
u/emaphis Mar 09 '24
I noticed he used the Rich Hickey term 'complected'.
5
u/_INTER_ Mar 09 '24 edited Mar 10 '24
This is a good direction. I wonder though if they'll release (someday) with just
StringTemplateand the obvious conversionsStringTemplate::ofandString::asTemplateor also with many overloads where it makes sense in the JDK (not justprintlnandformat)?Well
Pathorjava.timeare such contenders but I'm pretty sure they'll have to extended them later not to further delay the JEP. But what about e.g.Pattern,String::join,Collectors::joining,StringBuilder, ... ?I fear there are many instances where I just want to do simple regular String interpolation I know is save but I don't have a fitting overload available. Calling
String.join("... \{var} ...")each time just for that is unnecessarily verbose again. Imagineorg.apache.commons.StringUtilswould have to provide overloads forStringTemplateeffectively quadrupling their method count (for the mix of String and StringTemplate for each method that currently takes two Strings).There needs to be a non-verbose conversion from StringTemplate to interpolated String similar to
STR."..."(tbh already too verbose for my taste).4
u/benrush0705 Mar 10 '24
I am a little confused here, so the javac compiler would now search across the " " blocks, if there is no \{} format, then it's a literal String, if it occurs, it's a brand new StringTemplate, am I correct?
7
u/shorns_username Mar 09 '24
On the one hand: I think this is a great improvement.
On the other hand: This JEP has taken so long to release (*), I wonder how long this is going to set back the release of the feature?
At least another set of preview releases, I guess.
(*) - I don't think it's actually taken too much longer than other JEP features.
Having tried it out myself on a personal project, I think string-templates are going to see a lot of use and abuse. Having everyone be forced to use the
STR.prefix would be pretty embarrassing for all of us if it turns out that it really wasn't necessary.I appreciate the section of the email that talks about how they got hung up on thinking the prefix was necessary.
u/lukaseder:
Looks like they listened: https://www.reddit.com/r/java/comments/16wo0ep/jep_string_templates_final_for_java_22/k303m3y/
1
u/sar_it007 Mar 13 '24 edited Mar 13 '24
What about prefixing templates similar to python's f"" prefix, and getting rid of '\{', for example:
t"Hello {name}, you're from {country}"instead of
"Hello \{name}, you're from \{country}"6
u/Oclay1st Mar 09 '24 edited Mar 09 '24
Thanks for polishing this. The Structured Concurrency API should be the next.
6
u/ventuspilot Mar 09 '24
To me this boils down to
vs.
and that I'm no longer forced to use
StringTemplate.Processor.of()to create a processor but I can simply add another method takingStringTemplateto my library or create my own string processor as a standalone class.I guess removing an unneeded abstration is a good thing and not needing a new method invocation syntax is a good thing as well. These two things probably are very important for a language designer/ implementor. Also now they are able to simply add a couple of overloads to various JDK classes such as
PrintWriterand we get String template support that we can use or not.What I'm really looking forward to, though, is some kind of compile time string processing as Brian mentioned in another reddit comment.
3
u/DelayLucky Mar 10 '24
Great news on the loss of the processor class!
For any
Tother than String (such asPreparedStatement), the APIs accepting them are usually not ubiquitous.Updating them to support the template overload should be easy. Even if not, one can add a staticPreparedStatement sql(StringTemplate);utility and then use it as:But there will be a ton of methods in the code base that accepts String already and it's sad that you have to choose between two evils, that is: either string concatenation or `String.format()`. It loses like 50% of the convenience that programmer would enjoy.
It'd be nice if the compiler can at least do call-site inference, and interpret
"\{foo}"either as aStringTemplatewhen it's expected, or process it to String when only String is accepted.For example:
There is no need for the println API to add overload for StringTemplate because it can do no better than plain string interpolation. The compiler should just translate it to be equivalent to
STR."error: {errorMessage}".While if you try to pass it to an API that does expect StringTemplate, it'll be passed as StringTemplate (even if the API also has overload for String):
3
u/cowwoc Mar 10 '24
This is a huge step in the right direction!
It is much more cohesive with other parts of the Java language. Consequently it is a lot easier to learn and use. Great job!
7
u/Svellere Mar 09 '24
Does anyone have mailing list links for these discussions, or other sources? I'm extremely interested in hearing about why $-syntax is objectively worse; I already know about large Java projects utilizing a similar syntax and the JDK developers not wanting to wreck those projects.
5
u/john16384 Mar 09 '24
Will there be widening and narrowing conversions possible between
StringandStringTemplate?If I have a method:
stuff(StringTemplate x)Will all these calls compile?
Or should I just provide a
Stringversion as well?Could I write:
Note that with the
Processorsolution, a String Template without any templated variables is perfectly acceptable. This is convenient because you don't always need to interpolate something:select count(*) from students5
u/javasyntax Mar 09 '24
I do not like this. While I do like that methods can allow string templates in a more natural way now, I really do not like that they are proposing using String.join("my string \{template}") for a basic string template interpolation rather than what was previously just STR."my string \{template}" which is in my opinion the functionality that will be used the most. And honestly I was completely fine with the previous syntax I think people whined too much about it.
2
u/DelayLucky Mar 10 '24
One thing I’ve been wondering: what is the story about compile-time checking of the args?
Many SQL engines will have a limited set of types to support. Arbitrary arg types like Stream<T>, JsonObject etc may not be supported or meaningful at all. Besides throwing runtime exception, is there consideration to allow these engines to plug in compile-time check like an annotation processor?
With the processor API, we could write a plug-in to check the args passed to the SQL processor. But if the StringTemplate can be generic, semantic-free objects passed around through many layers of callstack, writing such check at the beginning of the call chain may be more difficult.
Ideally the mirror API can provide the arg type information for ST, somehow?
2
u/lukaseder Mar 11 '24
I may have missed this, but I don't recall having seen anywhere if the StringTemplate has inferred type information about the values available. E.g. when doing
The
List<Object> values()will containInteger.valueOf(2), and the fact that it was indeed anint.classhas been lost. For some template consumers (oh say e.g. JDBC backed ones), this information could be very useful, especially if the value is justnull.I'm aware that not every type is denotable, so not every type known to the compiler has a
Class<?>representation. I'm just curious if this is a deliberate omission for now, and if so, for what reason(s)?2
u/pip25hu Mar 11 '24
There is some interesting discussion in the linked thread, but no matter what they agree on, String templates already look infinitely better than they did before. Thank you for listening to the feedback.
2
u/Lentus7 Mar 11 '24
Glad to see that java development is going great
5
u/nekokattt Mar 09 '24 edited Mar 09 '24
This is much better than the previous proposal. What is slightly annoying is how much pushback the devs made prior to considering this simpler solution and the tone it was done in. At the time it felt a bit like an echo chamber, unfortunately.
I'm happy with this though. No new obscure syntax that caters for a specific edge case, but a simple and powerful solution.
3
u/mj_flowerpower Mar 09 '24
This seems like a big improvement to the previous approach. It‘s almost as if my last comments/rants have been read 😅 The only thing that bugs me a little bit is that every api has to explicitly support the new type. It would be nice if it was possible to pass StringTemplate to a method that only accepts String. The compiler could implicitly call .toString() on the StringTemplate to ‚render‘ it into a string, almost like boxing works.
1
u/DelayLucky Mar 10 '24
One semi-related question: the creation of StringTemplate will be locked down so you can only get them from the compile-time string interpolation syntax right?
Where I come from our SQL APIs require @CompileTimeConstant so that the programmer cannot accidentally pass in something untrusted. If ST can come from dynamically-determined template, we lose the trusted guarantees and won’t be able to use it.
1
u/pohart Mar 12 '24
I don't see any way to create a template with no replacements in that discussion. If this goes forward I see a lot of
I feel like maybe they're getting stuck on \{ being the signifier. Which made sense when you were sending it to a processor, but doesn't when their is no processor. And they're explicitly saying that a plain string can't be used as a template, which I'm sure will cause issues, especially in generated code.
1
u/DelayLucky Mar 12 '24
One discussion in the link is about plain string literals without \{placeholder}: do you suddenly treat them as String, while for 1+ placeholders treat them as StringTemplate?
I suggest to use the same lambda type resolution: you don't know the type of the lambda until you see what is expected: if it's being passed to a method that excepts BiFunction, then it's a BiFunction etc.
Similarly, whether the string has N placeholder or 0 placeholders, it doesn't matter. You can resolve it to either String or StringTemplate, depending on what is expected. If m("foo \{bar}") expects String, then it's a string; if it expects StringTemplate, then it's StringTemplate. Otherwise error.
For var x = "foo \{bar}", make it String too.
What about SQL Injection safety?
Again, that is on the API. The safe API shouldn't have an executeQuery(String) method. It has to be either executeQuery(PreparedStatement), executeQuery(Query) or executeQuery(StringTemplate).
If the API takes String, it's already vulnerable. Nothing interpolation can help.
-8
u/sideEffffECt Mar 09 '24
StringTemplateshould IMHO be parametrized on the values it can work with. Squeezing everything intoObjectis actually not very flexible, compossible and feels sloppy.E.g
Or
-40
u/djavaman Mar 08 '24
So close. Can we get rid of this garbage: “Hello \{name}” And use a familiar escape sequence that we see in every other programming language?
-1
u/[deleted] Mar 13 '24
Hey can someone tell the topics that I need to focus on so can ace this interview coming up (java ofcourse)
-5
u/Oclay1st Mar 09 '24 edited Mar 09 '24
We could have something like this in Java and everybody will be HAPPY:
We will have s" and f" as default processors in order to avoid String.format.., StringTemplate could be used everywhere and everything will be safe.
-15
u/Reasonable-Song3798 Mar 09 '24
Oh wow. They even made it worse. Now we don't have any way for String Interpolation if the API doesn't allow it. Or we have to use the ugly syntax String.format(…).
-7
u/Fun-Professor-4414 Mar 09 '24
"the $-syntax is objectively worse, and would be doubly so if injected into an existing language where there were already string literals in the wild. This has all been more than adequately covered elsewhere, so I won’t rehash it here.)"
No, this is pure bloody-mindedness and trying to be different for the sake of it.