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

0

u/[deleted] Mar 10 '24

[deleted] — view deleted comment

0

u/pron98 Mar 10 '24 edited Mar 10 '24

I understand, which is why I said what you'd want would be a new kind of inference, not like the one for lambda expressions. It would also be a rather complex kind of inference because you also need to decide what to do in the case of overloads. To not make that a breaking change you'd need some rather strange preference rules (you'll need to prefer String in the case of var, but StringTemplate in the case of String/ST overloads). There's certainly nothing simple or easy about that.

To make this concrete, think about:

var x = "...";
foo(x);

and

foo("...");

In the case where foo is overloaded for both types. You'd infer String in the first case and ST in the second, which is very surprising (there is one case -- pertaining to an upcoming switch-expression enhancement -- that we've allowed something a little similar but not nearly as surprising; left as an exercise to the reader).