Finalizing in JDK 16 - Pattern matching for instanceof
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Sep 10 20:38:14 UTC 2020
On 26/08/2020 16:00, Brian Goetz wrote:
> While I get why some people would like to bootstrap this into an
> argument why the pattern semantics are wrong, the key observation here
> is: _both of these questions are stupid_. So I think there's an
> obvious way to fix this so that there is no problem here: instanceof
> must ask a question. So the second form would be illegal, with a
> compiler error saying "pattern always matches the target."
>
> Proposed: An `instanceof` expression must be able to evaluate to both
> true and false, otherwise it is invalid. This rules out strongly
> total patterns on the RHS. If you have a strongly total pattern, use
> pattern assignment instead.
I think it seems ok - and rather obvious when looking at the Object case.
The issue is always gonna be with things like:
x() instanceof Foo
And then return type of x() changes from Object to something else, thus
creating some effect at distance.
But, we already have effect at distance of this kind in switch, for
nullity analysis (right?), so it's not like this is adding something new
(although it will create new source compatibility scenario for existing
code).
Maurizio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20200910/a9d71840/attachment.htm>
More information about the amber-spec-experts
mailing list