Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8310128

Switch with unnamed patterns erroneously non-exhaustive

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 22
    • 21
    • tools
    • 21
    • b03

        sealed interface Animal permits Dog, Chicken {};
        record Dog(String name, int age) implements Animal {};
        record Chicken(String name, int age) implements Animal {};

        void main() {
            Animal a1 = new Dog("Yodel", 4);
            Animal a2 = new Chicken("Purple Head", 5);

            switch (a1) {
                case Dog(var name, _) -> System.out.println("Dog: " + name);
                case Chicken(var name, _) -> System.out.println("Chicken: " + name);
                // default -> System.out.println("Unknown animal");
            }
        }

              abimpoudis Angelos Bimpoudis
              abimpoudis Angelos Bimpoudis
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: