<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi,<br>
      would some of the comments in this thread change if the example
      was more like:</p>
    <p>AlienSpaceShip ship = ...<br>
      int x = (int)ship.getX();</p>
    <p>This works and no error is issued.</p>
    <p>From a design perspective, the above code is basically what the
      record pattern is supposed to desugar to - in other words, pattern
      matching is precondition for safe cast. For primitives,
      determining whether a cast is "safe" involves a runtime range
      check, which is invoked by the compiler.</p>
    <p>So</p>
    <p>```<br>
      jshell> double d = 4.2;<br>
      d ==> 4.2<br>
      <br>
      jshell> d instanceof int<br>
      $2 ==> false<br>
      <br>
      jshell> double d = 4.0;<br>
      d ==> 4.0<br>
      <br>
      jshell> d instanceof int<br>
      $4 ==> true<br>
      ```</p>
    <p>This seems consistent. So the question is: what is the "mistake"
      we're talking about here? The user thinking it was a
      _unconditional_ match where in reality that's not the case? While
      in the case of instanceof unconditionality is harder to spot, if
      this was a switch, the compiler would have immediately asked the
      user to provide a default?</p>
    <p>It seems to me that the reasoning here implies that developers
      will use "if" + "instanceof" _without an "else" just for the
      purpose of extract some data, and then complain when the thing
      doesn't match (because they have no "else", and so their code will
      fail). It seems like our best weapon in this case is education,
      rather that introducing arbitrary restrictions and asymmetries in
      the language?</p>
    <p>(Or, perhaps, in the future have a way to perform an
      _unconditional_ extraction without an "if", in which case the
      compiler will bark if the pattern is not unconditional.)</p>
    <p>Maurizio<br>
    </p>
    <div class="moz-cite-prefix">On 04/02/2025 16:00, Tagir Valeev
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAE+3fjZM_QbUR-kQ-F8gMOt7rgMuU1cVSmkxFJzi1umt3z34hw@mail.gmail.com">
      
      <div dir="ltr">
        <div>Hello!</div>
        <div><br>
        </div>
        Well, I would say that Remi has a point. This is a silent
        mistake that can be made. You don't see the declaration, as it's
        in another file so you may forget about original types and
        assume that they are ints. The code compiles and doesn't even
        throw an exception, it just works incorrectly. It would be
        justified if such code pattern (converting from double to int
        only when double fits the int) was common, so conditional
        narrowing could be useful in many other places. But the fact is
        that it's highly uncommon. Nobody does this. Many other pattern
        matching features are nice. E.g., I often see the code which can
        be migrated to pattern switch (we are still on Java 17) and I
        really want to use it. However, I never see the code which can
        be migrated to a narrowing primitive pattern.
        <div><br>
        </div>
        <div>I don't agree that we should drop primitive patterns
          completely, but we may reconsider floating point <->
          integral conversions and disable them in patterns for a while
          (enabling them back later will be always possible).</div>
        <div><br>
        </div>
        <div>With best regards,</div>
        <div>Tagir Valeev</div>
      </div>
      <br>
      <div class="gmail_quote gmail_quote_container">
        <div dir="ltr" class="gmail_attr">On Tue, Feb 4, 2025 at
          12:45 PM Brian Goetz <<a href="mailto:brian.goetz@oracle.com" moz-do-not-send="true" class="moz-txt-link-freetext">brian.goetz@oracle.com</a>>
          wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Wow,
          that was pretty amazing. You went from “one student, who was
          never taught about how this works, was confused by it” to “
          let’s pull the whole feature “ in one breath.  <br>
          <br>
          > On Feb 4, 2025, at 11:33 AM, Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">forax@univ-mlv.fr</a>>
          wrote:<br>
          > <br>
          > Last week,<br>
          > one of the TA for the course "OOP using Java" was grading
          student projects,<br>
          > the subject of the project can be summarize to "you are
          alone in space and angry aliens want you to die".<br>
          > <br>
          > One of the project had a weird bug when displaying
          aliens.<br>
          > Spoiling the result of bug hunting, this is due to
          students using the pattern matching on primitive without them
          realizing it.<br>
          > <br>
          > Here is a reproducer of the bug:<br>
          > <br>
          > // in AlienSpaceship.java<br>
          > record AlienSpaceship(double x, double y) implements
          Spaceship {<br>
          >  ...<br>
          > }<br>
          > <br>
          > ...<br>
          > <br>
          > // in a method in another class<br>
          >  ...<br>
          >  for(Spaceship spacehip : ...) {<br>
          >    ...<br>
          >    if (spaceship instanceof AlienSpaceship(int x, int y)
          {<br>
          >      // display the alien space ship at (x, y)<br>
          >    }<br>
          >    ...<br>
          >  }<br>
          > <br>
          > The TA said she was about to give up when she found the
          issue and that the Java spec should not allow such pattern.<br>
          > I agree with here, pattern matching on primitive types is
          not a feature people ask for and mixed with instanceof it's a
          footgun.<br>
          > <br>
          > This feature can be re-introduced later using method
          patterns and it will be easier to understand the code,<br>
          > by example with an hyphotethical method Double.asInt<br>
          > <br>
          >  if (spaceship instanceof AlienSpaceship(Double.asInt(int
          x), Double.asInt(int y)) {<br>
          >    ...<br>
          > <br>
          > regards,<br>
          > Rémi<br>
        </blockquote>
      </div>
    </blockquote>
  </body>
</html>