<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">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">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>