Record Pattern Bug in Java 19

Remi Forax forax at univ-mlv.fr
Wed Jul 27 18:35:10 UTC 2022


> From: "Clayton Wohl" <claytonwohl at gmail.com>
> To: "amber-dev" <amber-dev at openjdk.org>
> Sent: Wednesday, July 27, 2022 8:26:52 PM
> Subject: Record Pattern Bug in Java 19

> // The example given in Brian Goetz's article: [
> https://www.infoq.com/articles/data-oriented-programming-java/ |
> 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.

oops, i can reproduce it 

~/jdk/jdk-19.jdk/Contents/Home/bin/javac --enable-preview -source 19 Opt.java 
Opt.java:7: error: the switch statement does not cover all possible input values 
switch (optValue) { 
^ 
Note: Opt.java uses preview features of Java SE 19. 
Note: Recompile with -Xlint:preview for details. 
1 error 

Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20220727/b1f1fc68/attachment.htm>


More information about the amber-dev mailing list