<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4" face="monospace">This doesn't really have as much to
do with sealed types or pattern matching as it might seem; it has
to do with to what degree the language is willing to engage in
flow-based type analysis. <br>
<br>
The short answer is: there's not a lot of appetite for investing
in this. The cost and complexity is likely high both initially
and ongoing, and the impact on program correctness is fairly
marginal. (The simple cases are easy, but just handling the
simple cases is a recipe for an infinite stream of enhancement
requests.) <br>
<br>
If we're going to invest at all in flow-based type analysis, we'd
likely start with nullity analysis. This is a simpler problem
(and still, not simple) and probably would have more impact. <br>
</font><br>
<div class="moz-cite-prefix">On 4/19/2025 12:17 PM, Andreas Berheim
Brudin wrote:<br>
</div>
<blockquote type="cite" cite="mid:CAKJ_Mj717vW8yeYi7etZvniibRzCuamu58vN7HpkKUZ5oUSo2A@mail.gmail.com">
<div dir="ltr">
<div>Hi all,<br>
<br>
I'm new to the list—apologies if this has been discussed
before, and thanks in advance for your time.<br>
<br>
I have a question about pattern matching with sealed types.
Consider this example:<br>
<br>
sealed interface A permits B, C {}<br>
record B(String b1, String b2) implements A {}<br>
record C(String c) implements A {}<br>
<br>
A myVar = ...;<br>
if (!(myVar instanceof B(var b1, var b2))) {<br>
return switch (myVar) {<br>
case C(var c) -> c;<br>
case B b -> throw new IllegalStateException("should
not happen");<br>
};<br>
}<br>
// use b1, b2<br>
<br>
Here, I want to keep an early-return style, but since I need
to return a value from C, I have to use a switch. This leads
to redundancy: we've already tested myVar is not a B, so that
case should be statically unreachable.<br>
<br>
My questions:<br>
<br>
1. Is there any consideration or ongoing work to allow the
compiler to automatically eliminate such unreachable cases?<br>
<br>
2. If only one case remains (in this case, C), could it be
possible to treat the variable as a C directly without
requiring an explicit switch?<br>
<br>
Again, apologies if this has already been discussed. I'd
appreciate any pointers to relevant threads or JEPs if so.<br>
<br>
Thanks,<br>
</div>
Andreas</div>
</blockquote>
<br>
</body>
</html>