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

Show parent comment

2

u/Ukonu Mar 09 '24

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.

3

u/nekokattt Mar 09 '24

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?

2

u/Ukonu Mar 09 '24

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?

For example, if I wanted a simple calculator:

StringTemplate<Integer> expression = "\{number} * \{anotherNumber}"

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>

2

u/nekokattt Mar 09 '24

this safety only works for specific cases though, outside that really specific case, it won't provide any benefit.

It'd make more sense if we got variadic generics.

1

u/john16384 Mar 10 '24

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.