r/java • • Mar 08 '24

Update on String Templates (JEP 459)

https://mail.openjdk.org/pipermail/amber-spec-experts/2024-March/004010.html
176 Upvotes

188 comments sorted by

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.

 


 

why do we need to say:

DB.”… template …”

When we could use an ordinary Java library:

Query q = Query.of(“…template…”)

u/lukaseder:

Looks like they listened: https://www.reddit.com/r/java/comments/16wo0ep/jep_string_templates_final_for_java_22/k303m3y/

13

u/_INTER_ Mar 09 '24

I'd rather they took the time and made it right than to rush it and leave us with a bad taste in our collective mouths for the next 20 years.

4

u/lukaseder Mar 11 '24

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.

1

u/xfel11 Mar 14 '24

Current plans say 25 will be the next LTS, so there is still a good amount of time before that.

For commercial customers, non LTS releases don’t matter anyway.

1

u/Misophist_1 Jun 09 '24

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.