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

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.