Why is this switch over sealed types not exhaustive?
Brian Goetz
brian.goetz at oracle.com
Tue Jun 15 16:35:39 UTC 2021
This looks like a bug. D and E together cover C (since C is sealed *and
abstract*), and B and C cover A (same reason.) If either A or C were
not abstract, you'd need a case for those.
On 6/15/2021 12:31 PM, Cay Horstmann wrote:
> 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
>
More information about the amber-dev
mailing list