<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Jordan Zimmerman" <jordan@jordanzimmerman.com><br><b>To: </b>"amber-dev" <amber-dev@openjdk.org><br><b>Sent: </b>Monday, August 1, 2022 1:58:24 PM<br><b>Subject: </b>Possible Pattern Matching for switch (Third Preview) bug<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">There doesn't seem to be a way to have a switch case pattern for multiple related patterns. Given that it works in an instanceof pattern I would think it might work in a switch pattern. But, maybe not. Anyway here's what I found.<div class=""><br class=""></div><div class="">Given:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">public interface MyFace {}</div><div class=""><br class=""></div><div class="">public record MyEye() implements MyFace {}</div><div class="">public record MyNose() implements MyFace {}</div><div class=""><br class=""></div><div class="">public void examine(Object face) {</div><div class="">switch (face) {</div><div class="">case MyEye eye, MyNose nose -> System.out.println("part of my face");</div><div class="">default -> System.out.println("Not part of my face");</div><div class="">}</div><div class="">}</div><div class=""><br class=""></div></blockquote><div class="">This produces: "illegal fall-through to a pattern".</div><div class=""><br class=""></div><div class="">However, this works with an instanceof pattern. E.g.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">public void examine(Object face) {</div></div><div class=""><div class="">if ((face instanceof MyEye eye) || (face instanceof MyNose nose)) {</div></div><div class="">System.out.println("part of my face");</div><div class="">}</div><div class="">else {</div>System.out.println("Not part of my face");<div class="">}</div><div class=""><div class="">}</div></div></blockquote><div class=""><br class=""></div><div class="">Of course, the instanceof test is not very useful as the bound variables "eye" or "nose" are only scoped to the immediate test (and not in the if block). So, this may not be a bug? Anyway, I thought I'd mention it. For the switch it would be useful to have common behavior for several related patterns without having to use a method to do it. The bound variables would be ignored (maybe via an upcoming wonderbar "_").</div></blockquote><div><br></div><div>yes, that's one possible future, introduce '_' so one can write<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><div class="">public void examine(Object o) {<br></div><div class=""> switch (o) {</div><div class=""> case MyEye _, MyNose _ -> System.out.println("part of my face");</div><div class=""> default -> System.out.println("Not part of my face");</div><div class=""> }</div><div class="">}</div><div class=""><br data-mce-bogus="1"></div></div><div><br data-mce-bogus="1"></div><div>But we have to answer to the question: where we can put '_' ? as method parameter, lambda parameter, catch parameter, local variable, etc<br data-mce-bogus="1"></div><div>Perhaps we do not need '_', and it's "better" to allow the type pattern to be declared without a binding like the record pattern has an optional binding for the whole pattern ?<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div class=""><br class=""></div><div class="">Cheers.</div><div class=""><br class=""></div><div class="">-Jordan</div><div class=""><br class=""></div></blockquote><div><br></div><div>regards,<br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>