<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div>[...]<br></div><div data-marker="__QUOTED_TEXT__"><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;"><blockquote cite="mid:1724934379.19079517.1659732755546.JavaMail.zimbra@u-pem.fr"><blockquote><pre class="moz-quote-pre">Situation would, of course, be different if we tried to optimize not
only prefixes of the patterns - but this does not seem to be the
proposal in switch-pattern-combinator.


As for casting to the last permitted type - I don't see how this relates
to decision trees.  While casting and catching might look interesting
from the bytecode perspective, it is something that is likely to be
relatively complex to implement in javac, and using an (effective)
instanceof and an explicit fallback does not really sound terrible.
</pre>
      </blockquote>
      <pre class="moz-quote-pre">The idea of using a cast is that you provide a good error message to the users because the ClassCastException stores the class that is not handled by the pattern matching.
You loose that information if you use an explicit fallback.</pre>
    </blockquote>
    <p><br>
    </p>
    <p>Sorry, but if I understand what you are proposing is:</p>
    <p>B b = (B) obj; //magical catch handler somewhere handling the CCE</p>
    <p>$rest$;<br>
    </p>
    <p><br>
    </p>
    <p>what I am saying is that this can be achieved by an analog of:</p>
    <p>if (obj instanceof B b) {</p>
    <p>    $rest$;</p>
    <p>} else {</p>
    <p>     throw <an-exception-we-want>(obj.getClass());</p>
    <p>}</p>
    <p><br>
    </p>
    <p>What information is lost?</p></blockquote><div><br></div><div>sorry I misunderstood you.</div><div>There is no information lost in this case, the generated bytecodes is just bigger which one one reason cited by Brian to only throw once in the fallback case.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Nitpicking, "null" is treated differently so the code should be<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>  if (obj == null) {<br data-mce-bogus="1"></div><div>    throw NPE;<br data-mce-bogus="1"></div><div>  }<br data-mce-bogus="1"></div><div><p style="margin: 0px;" data-mce-style="margin: 0px;">  if (obj instanceof B b) {</p><p style="margin: 0px;" data-mce-style="margin: 0px;">      $rest$;</p><p style="margin: 0px;" data-mce-style="margin: 0px;">  } else {</p><p style="margin: 0px;" data-mce-style="margin: 0px;">     throw <an-exception-we-want>(obj.getClass());</p><p style="margin: 0px;" data-mce-style="margin: 0px;">  }</p><p style="margin: 0px;" data-mce-style="margin: 0px;"><br data-mce-bogus="1"></p></div><div>Using a cast also has an issue with null but the NPE can be thrown implicitly if the sub-pattern is a record (something you know locally for each node of the decision tree).<br data-mce-bogus="1"></div><div><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;"><p><br>
    </p>
    <p><br>
    </p>
    <blockquote cite="mid:1724934379.19079517.1659732755546.JavaMail.zimbra@u-pem.fr">
      <pre class="moz-quote-pre"></pre>
      <blockquote>
        <pre class="moz-quote-pre">There is also a question on when this is safe to do - if there's a `case
Object o` at the end of the switch, then probably this is not correct to
do anyway, etc.
</pre>
      </blockquote>
      <pre class="moz-quote-pre">It is safe to do if the compiler has type-checked the patterns before, the cast is only done in case of a sealed type, not if there is a common supertype at the end.</pre>
    </blockquote>
    <p><br>
    </p>
    <p>I mean - consider:</p>
    <p>sealed interface I {}</p>
    <p>final class OnlySubtypeAtCompileTime implements I {}</p>
    <p>record Box(I i) {}</p>
    <p><br>
    </p>
    <p>Object b = new
      Box(<a-new-subtype-not-known-at-compile-time>);</p>
    <p>switch (b) {</p>
    <p>     case Box(OnlySubtypeAtCompileTime t) ->
      System.err.println("Should probably not fail here,");<br>
    </p>
    <p>     case Object obj -> System.err.println("but should end up
      here.");</p>
    <p>}</p></blockquote><div><br></div><div>I think we are agreeing here that in this case, using a cast instead of an instanceof is a mistake. We only need the cast in case if there is a sealed type and no fallback, this information has flow from the type-ckecking.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>RĂ©mi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div><br></div></div></div></body></html>