<div dir="ltr"><div>// The example given in Brian Goetz's article: <a href="https://www.infoq.com/articles/data-oriented-programming-java/">https://www.infoq.com/articles/data-oriented-programming-java/</a><br>sealed interface Opt<T> {<br> record Some<T>(T value) implements Opt<T> { }<br> record None<T>() implements Opt<T> { }<br>}<br><br>// This works: Exhaustive switch without default case, but no record pattern<br>public static void thisWorks1(int value) {<br> Opt<String> optValue = doSomethingThatReturnsOpt(value);<br> switch (optValue) {<br> case Opt.Some<String> some -> System.out.printf("got string: %s%n", some.value());<br> case Opt.None<String> none -> System.out.println("got none");<br> };<br>}<br><br>// This works: record pattern in a switch statement with a default case.<br>public static void thisWorks2(int value) {<br> Opt<String> optValue = doSomethingThatReturnsOpt(value);<br> switch (optValue) {<br> case Opt.Some<String>(String v) -> System.out.printf("got string: %s%n", v);<br> case Opt.None<String> none -> System.out.println("got none");<br> default -> System.out.printf("default%n");<br> };<br>}<br><br>// This does NOT compile: Exhaustive switch without default case + record pattern<br>public static void thisDoesNotWork(int value) {<br> Opt<String> optValue = doSomethingThatReturnsOpt(value);<br> switch (optValue) {<br> case Opt.Some<String>(String v) -> System.out.printf("got string: %s%n", v);<br> case Opt.None<String> none -> System.out.println("got none");<br> };<br>}<br></div><div><br></div><div>This is with the latest public JDK 19 build: <span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0);font-family:Menlo;font-size:11px">build 19-ea+32-2220</span></div>
<div><br></div>I hope I'm posting to the correct list. If this list is for internal Java developers only, I'm sorry.</div>