Update on String Templates (JEP 459)
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Mar 14 22:07:03 UTC 2024
On 14/03/2024 20:53, Clement Cherlin wrote:
> I'm going to assume you're joking here, so I don't feel the need to
> write a thousand words about how terrible Java's casting syntax is.
Honestly no, I wasn’t joking, at least not from a semantic perspective.
Let me see if I can explain myself.
Let’s say a string interpolation literal is spelled like:
|String"my name is \{name}" |
What is this expression doing? Well, it’s taking some literal that looks
like a string, but has some embedded expression, and explicitly ask to
turn that thing into a String…
Now, isn’t that what (morally) a cast is for? E.g. the object you are
casting has a type (StringTemplate) and you want to turn it into
something else (String). Seems quite close. And, while cast between
reference types don't do much (beside changing the type), cast between
primitives, or between primitives and references (boxed types) do end up
changing the underlying representation. So again, semantically we're not
too far. Where I think cast is a bad fit is that we can't use the cast
syntax just as a syntactic device. If cast syntax works, it means
there's a casting conversion between String and StringTemplate which
means (as I mentioned the other day) that pattern matching will need to
come along for the ride too.
In terms of syntax, I might agree with you that it’s not a great option,
but the “conversion vibe” that a cast gives isn’t totally off the mark.
Maurizio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20240314/87a34862/attachment.htm>
More information about the amber-spec-observers
mailing list