Pattern matching - exhaustiveness is botched

Remi Forax forax at univ-mlv.fr
Mon Jun 7 08:51:49 UTC 2021


I don't know if you know but using the latest version of the source,
this code does not compile.

  sealed interface Vehicle {}
  record Car(String owner, String color) implements Vehicle {}
  record Bus(String owner) implements Vehicle {}

  public static void example2() {
    var vehicles = List.of(
        new Car("Bob", "red"),
        new Bus("Ana")
    );
    for(var vehicle: vehicles) {
      switch(vehicle) {
        case Car car -> System.out.println("car !");
        case Bus bus -> System.out.println("bus !");
        //default -> throw new AssertionError();
      }
    }
  }


PatternMatching101.java:25: error: the switch statement does not cover all possible input values
      switch(vehicle) {
      ^

same issue with a switch expression.

regards,
Rémi


More information about the core-libs-dev mailing list