<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
The switch behavior is as intended. The instanceof behavior is
slightly surprising, but harmless -- basically like a dead local
variable. But in both cases, a dead binding is unlikely to mean
what you intend. <br>
<br>
As you surmise, the restriction for switch has more to do with
bindings than with patterns; the case label is unable (or unwilling)
to define bindings that cannot be used in the case block. Currently
we express this restriction with the grammar (one pattern per case),
but we may relax this in the future and instead enforce the
restriction based on DA of bindings, in which case you might be able
to do something like:<br>
<br>
case Foo _, Bar _:<br>
<br>
A related possibility is that it may be possible to _merge_
bindings:<br>
<br>
case Box(String x), Bag(String x): System.out.println(x);<br>
<br>
Here, we would match one pattern or the other, and either way, one
of them would bind `String x`. We discussed this for a while but
deferred it for later. The main challenge here is: "where's the
declaration of x?" This may be confusing to both humans and tools,
to have two declarations for a common use. <br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 8/1/2022 7:58 AM, Jordan Zimmerman
wrote:<br>
</div>
<blockquote type="cite" cite="mid:C2F8432E-9E8A-41BA-876C-49B9EB987F8C@jordanzimmerman.com">
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=""><span class="Apple-tab-span" style="white-space:pre"> </span>switch
(face) {</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>case
MyEye eye, MyNose nose -> System.out.println("part of my
face");</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>default
-> System.out.println("Not part of my face");</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>}</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=""><span class="Apple-tab-span" style="white-space: pre;"> </span>if
((face instanceof MyEye eye) || (face instanceof MyNose
nose)) {</div>
</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("part
of my face");</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>}</div>
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>else
{<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("Not
part of my face");
<div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>}</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>
<div class=""><br class="">
</div>
<div class="">Cheers.</div>
<div class=""><br class="">
</div>
<div class="">-Jordan</div>
<div class=""><br class="">
</div>
</blockquote>
<br>
</body>
</html>