Why is this switch over sealed types not exhaustive?

Cay Horstmann cay at horstmann.com
Tue Jun 15 16:31:35 UTC 2021


Hi, I was surprised that the following switch expression "does not cover 
all possible input values", according to JDK build 17-ea+26-2439.

sealed abstract class A permits B, C {}
final class B extends A {}
sealed abstract class C extends A permits D, E {}
final class D extends C {}
final class E extends C {}

public class Test {
    public static void main(String[] args) {
       A a = new D();
       System.out.println(
          switch (a) {
             case B x -> "B";
             case D x -> "D";
             case E x -> "E";
          });
    }
}

However, the more combersome

          switch (a) {
             case B x -> "B";
             case C c -> switch (c) {
                case D x -> "D";
                case E x -> "E";
             };
          }

is deemed exhaustive. Is that a bug or does it work as designed?

Thanks,

Cay

-- 

Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com


More information about the amber-dev mailing list