switch exhaustiveness check not working within deconstruction pattern.

redio.development at gmail.com redio.development at gmail.com
Sun Nov 13 17:48:55 UTC 2022


package dev.redio;

 

import static dev.redio.Main.Activity.*;

 

public class Main {

 

    public static void main(String[] args) {

        record Pair(boolean weekend, Activity activity) {}

        var pair = new Pair(true, new Sleeping(8));

        var msg = switch (pair) {   // <- A switch expression should have a
default case Java(1073743531)

            case Pair(boolean b, Sleeping s) -> "3";

            case Pair(boolean b, Skiing s) -> "4";

            case Pair(boolean b, Coding c) -> "5";

        };

        

        System.out.println(msg);

    }

 

    sealed interface Activity {

        record Sleeping(int hours) implements Activity {}

        record Skiing(String resort) implements Activity {}

        record Coding() implements Activity {}

    }

    

}

 

This deconstruction should be exhaustive since Pair is a guaranteed match
boolean doesn't have subclasses and Activity is sealed and fully covered. 

Null should throw in this case. A default case would never be reached. 

(Compiling and running with (dead) default case)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20221113/3eacfa50/attachment.htm>


More information about the amber-dev mailing list