[External] : Re: JEP 405 update

Brian Goetz brian.goetz at oracle.com
Thu Feb 10 17:12:51 UTC 2022


>> That’s really what I am getting at. In that sense, record patterns are a key, because they
>> support nesting of patterns. You are correct that the construct within which we
>> surface pattern matching can have a say in the top-level treatment of null.
> It's not what i've said, the treatment of null is due to the fact that a type pattern (at least the non total one) is semantically an instanceof thus reject null.

We have been over this over and over again.  This is not right, and you 
are confusing people.

There is a predicate for pattern matching, `matches(P, T, e)`.  It is 
defined only in terms of the pattern, the target type, and an expression 
to match against it.  If P="Object o", and T="String", then matches(P, 
T, null) is true.  This is the semantics of _patterns_.  Total patterns 
match null.

Patterns can appear in multiple _contexts_, such as instanceof and 
switch today, and soon, nested pattern contexts, and maybe in the 
future, catch clauses or pattern assignment.  The context can appeal to 
the semantics of pattern matching.  So for

     x instanceof P

the instanceof context first tests x for null, evaluating to false if 
so, and otherwise appeals to matches(P, typeof(x), x).  For switch, the 
switch context first tests the target for null, and, if the switch has 
no null cases, throws NPE, otherwise it starts appealling to pattern 
matching.  In a nested context, there is no such extra context-specific 
logic.  In other contexts, such as let-bind, there will be similar 
remainder-rejection.  But switch throwing NPE on null has nothing to do 
with whether the patterns match null.

The two are separate; you are conflating them.  I realize you would like 
it to work differently, but please stop incorrectly characterizing how 
it does work.

> yes i know, and i find that idea weird, we should try to make the 
> patterns to behave the same way whatever the container, to not bother 
> users with to many details. 

They do work the same regardless of container.  It is the *containers* 
that have their own additional logic, that is executed first, before we 
attempt to match any patterns.




More information about the amber-spec-experts mailing list