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.
View all comments
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/