<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Let’s dig in and see if we can find what’s really bothering you here.  (But, the clickbait subject and the escalation from “someone was confused” to “so let’s take it out” is surely not helping, so let’s leave that behind.)<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class=""><span style="font-family: arial, helvetica, sans-serif; font-size: 16px;" class="">For me it's more about the conversion being implicit vs explicit.</span></div>
</blockquote>
<div><br class="">
</div>
<div>But, I’m not seeing an implicit conversion.  I’m seeing an *explicit* one.  </div>
<div><br class="">
</div>
<div>If I say</div>
<div><br class="">
</div>
<div>    float f = …</div>
<div>    Int i = f</div>
<div><br class="">
</div>
<div>The conversion here is both implicit and *lossy*.  (I would be sympathetic if you were saying that lossy assignment conversion was troubling; this is a feature that we copied too-literally from C, and this is arguably a mistake.). But if I say</div>
<div><br class="">
</div>
<div>    If (f instanceof int i) { … }</div>
<div><br class="">
</div>
<div>The conversion looks pretty explicit to me, and it is never lossy.  This is strictly better than either of the alternatives (don’t allow the pattern match at all, in which case the only conversion available to the user is an unsafe cast, or always allow
 it but accept silent information loss.).  Further, I think a great deal of the discomfort here is that we’re still getting used to it.  The old way was both inflexible and unsafe, so we’ve gotten used to the idea that “everything about primitives is different.”
  Valhalla will bust that wide open soon enough anyway, so there’s no point in trying to cling to the old intuitions about primitives.</div>
<div><br class="">
</div>
<div>The other feature involved is pattern nesting, which is composition; x matches P(Q) when x matches P(a) && a matches q.  We discussed the concerns over “action at a distance” endlessly in the first go-around; some people were concerned that </div>
<div><br class="">
</div>
<div>    Box<Object> b = …</div>
<div>    If (b instanceof Box(String s)) { … }</div>
<div><br class="">
</div>
<div>Was too subtle, because the declaration and the use of the pattern were “far away” and a reader might not see the conditionality of the nested pattern.  I’m sympathetic to that, but (a) this is something that one grows more comfortable with with use, (b)
 IDEs can help by coloring conditional nested patterns differently, (c) the alternative is far less expressive.  </div>
<div><br class="">
</div>
<div>What we’re seeing now is merely composing these two features; there’s nothing new here, except that the visual ambiguity between “just unpack the pattern” and “do a nested match” gets combined with the novelty of primitive patterns (which again, take some
 time getting used to.)</div>
<div><br class="">
</div>
<div>I can see how you would call this an “implicit” conversion, but it’s not any more implicit with primitives as it is with Box(String s).  </div>
<div><br class="">
</div>
<div>So I recommend you think long and hard about what is *really* bothering you, and then we can have a conversation about it.</div>
<div><br class="">
</div>
<div><br class="">
</div>
<div><br class="">
</div>
<div><br class="">
</div>
<br class="">
<blockquote type="cite" class="">
<div class="">
<div data-marker="__QUOTED_TEXT__" style="caret-color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">
<div class="">Here, we have an implicit conversion while the conversion does not always succeed.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">I believe such conversions, the one that may not always succeed, should be explicit not implicit.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">regards,<br data-mce-bogus="1" class="">
</div>
<div class="">Rémi<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<div class="">@Brian, i'm more worry about a TA with more than 10 years of experience having trouble to find a bug in a student project written in Java.<br class="">
</div>
<div class="">@Tagir, i'm not sure usin floating point values is the issue here, you can have the same kind of bug between a character and an int.<br data-mce-bogus="1" class="">
</div>
<div class=""><br data-mce-bogus="1" class="">
</div>
<blockquote style="border-left-width: 2px; border-left-style: solid; border-left-color: rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-size: 12pt;" class="">
<div dir="ltr" class="">
<div class="gmail_quote gmail_quote_container"><br class="">
<br class="">
<br class="">
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;">
<div class="">
<div class="">On 04/02/2025 16:00, Tagir Valeev wrote:<br class="">
</div>
<blockquote class="">
<div dir="ltr" class="">
<div class="">Hello!</div>
<div class=""><br class="">
</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 class=""><br class="">
</div>
<div class="">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 class=""><br class="">
</div>
<div class="">With best regards,</div>
<div class="">Tagir Valeev</div>
</div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Tue, Feb 4, 2025 at 12:45 PM Brian Goetz <<a href="mailto:brian.goetz@oracle.com" target="_blank" class="">brian.goetz@oracle.com</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: 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. <span class="Apple-converted-space"> </span><br class="">
<br class="">
> On Feb 4, 2025, at 11:33 AM, Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank" class="">forax@univ-mlv.fr</a>> wrote:<br class="">
><span class="Apple-converted-space"> </span><br class="">
> Last week,<br class="">
> one of the TA for the course "OOP using Java" was grading student projects,<br class="">
> the subject of the project can be summarize to "you are alone in space and angry aliens want you to die".<br class="">
><span class="Apple-converted-space"> </span><br class="">
> One of the project had a weird bug when displaying aliens.<br class="">
> Spoiling the result of bug hunting, this is due to students using the pattern matching on primitive without them realizing it.<br class="">
><span class="Apple-converted-space"> </span><br class="">
> Here is a reproducer of the bug:<br class="">
><span class="Apple-converted-space"> </span><br class="">
> // in AlienSpaceship.java<br class="">
> record AlienSpaceship(double x, double y) implements Spaceship {<br class="">
>  ...<br class="">
> }<br class="">
><span class="Apple-converted-space"> </span><br class="">
> ...<br class="">
><span class="Apple-converted-space"> </span><br class="">
> // in a method in another class<br class="">
>  ...<br class="">
>  for(Spaceship spacehip : ...) {<br class="">
>    ...<br class="">
>    if (spaceship instanceof AlienSpaceship(int x, int y) {<br class="">
>      // display the alien space ship at (x, y)<br class="">
>    }<br class="">
>    ...<br class="">
>  }<br class="">
><span class="Apple-converted-space"> </span><br class="">
> 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 class="">
> 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 class="">
><span class="Apple-converted-space"> </span><br class="">
> This feature can be re-introduced later using method patterns and it will be easier to understand the code,<br class="">
> by example with an hyphotethical method Double.asInt<br class="">
><span class="Apple-converted-space"> </span><br class="">
>  if (spaceship instanceof AlienSpaceship(Double.asInt(int x), Double.asInt(int y)) {<br class="">
>    ...<br class="">
><span class="Apple-converted-space"> </span><br class="">
> regards,<br class="">
> Rémi</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br class="">
</body>
</html>