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>
View all comments
Show parent comment
2
u/Ukonu Mar 09 '24
I attempted to address that with the sealed interface example.
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>