Total pattern and null pattern

Gavin Bierman gavin.bierman at oracle.com
Wed Jul 20 10:14:40 UTC 2022


That looks like a compiler bug (probably a hangover from the previous preview where total patterns could match null). The current spec has no rules concerning dominance of null.

http://cr.openjdk.java.net/~gbierman/jep427%2b405/jep427+405-20220601/specs/patterns-switch-record-patterns-jls.html<http://cr.openjdk.java.net/~gbierman/jep427+405/jep427+405-20220601/specs/patterns-switch-record-patterns-jls.html#jls-15.28>

Thanks!
Gavin

On 20 Jul 2022, at 09:54, Tagir Valeev <amaembo at gmail.com<mailto:amaembo at gmail.com>> wrote:

Hello!

Playing with Java 19, we stumbled upon this sample:

public class Main {
 public static void main(String[] args) {
   Integer x = null;
   switch (x) {
     case Integer i -> System.out.println(i);
     case null -> System.out.println("null is supplied!");
   }
 }
}

Latest javac 19-ea says that this code is not compilable, as Integer i
is dominated by null. However, if we remove `case null`, then `case
Integer i` doesn't match the `null` (NPE is thrown). I find it strange
that `case Integer i` matches null when `case null` is supplied and
I'm forced to use `case null` before `case Integer i`. Is it expected
behavior?

With best regards,
Tagir Valeev.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20220720/c0973070/attachment.htm>


More information about the amber-spec-experts mailing list