Record Pattern Bug in Java 19
Brian Goetz
brian.goetz at oracle.com
Wed Jul 27 20:04:31 UTC 2022
Thanks for the bug report! This is the right place for bug reports like
this.
I believe this is the same as a bug we're currently working on; the way
we've specified exhaustiveness is almost, but not quite, right with
respect to generics. Stay tuned for an update.
On 7/27/2022 2:26 PM, Clayton Wohl wrote:
> // 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/2b576a34/attachment.htm>
More information about the amber-dev
mailing list