Using the Brian Goetz article: https://www.infoq.com/articles/data-oriented-programming-java/
sealed interface Opt<T> {
record Some<T>(T value) implements Opt<T> { }
record None<T>() implements Opt<T> { }
}
This compiles and runs as expected. The exhaustive pattern matching works:
Opt<String> optValue = doCalc(value);
switch (optValue) {
case Opt.Some<String> some -> System.out.printf("got string: %s%n", some.value());
case Opt.None<String> none -> System.out.printf("got none%n");
};
This variation where I use the new Record patterns preview feature, breaks the exhaustive pattern matching, where this won't compile without adding a default case statement:
Opt<String> optValue = doCalc(value);
switch (optValue) {
case Opt.Some<String>(String v) -> System.out.printf("got string: %s%n", v);
case Opt.None<String> none -> System.out.printf("got none%n");
};
With OpenJDK Runtime Environment (build 19-ea+32-2220)
, I get the compilation error: the switch statement does not cover all possible input values
.
When I add a default case statement, and the program works, but I don't get exhaustive pattern matching.
If I remove the record pattern matching, the program works.
If I create a variation of this without generics, that uses sealed classes, exhaustive pattern matching, and record patterns, it works.
However, it seems the combination of record patterns, generics and exhaustive pattern matching does not work.