Record Pattern Bug in Java 19
Clayton Wohl
claytonwohl at gmail.com
Wed Jul 27 18:26:52 UTC 2022
// The example given in Brian Goetz's 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 works: Exhaustive switch without default case, but no record pattern
public static void thisWorks1(int value) {
Opt<String> optValue = doSomethingThatReturnsOpt(value);
switch (optValue) {
case Opt.Some<String> some -> System.out.printf("got string: %s%n",
some.value());
case Opt.None<String> none -> System.out.println("got none");
};
}
// This works: record pattern in a switch statement with a default case.
public static void thisWorks2(int value) {
Opt<String> optValue = doSomethingThatReturnsOpt(value);
switch (optValue) {
case Opt.Some<String>(String v) -> System.out.printf("got string:
%s%n", v);
case Opt.None<String> none -> System.out.println("got none");
default -> System.out.printf("default%n");
};
}
// This does NOT compile: Exhaustive switch without default case + record
pattern
public static void thisDoesNotWork(int value) {
Opt<String> optValue = doSomethingThatReturnsOpt(value);
switch (optValue) {
case Opt.Some<String>(String v) -> System.out.printf("got string:
%s%n", v);
case Opt.None<String> none -> System.out.println("got none");
};
}
This is with the latest public JDK 19 build: build 19-ea+32-2220
I hope I'm posting to the correct list. If this list is for internal Java
developers only, I'm sorry.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20220727/d5f40ca4/attachment-0001.htm>
More information about the amber-dev
mailing list