<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="">
You’re right that §4.10.1 is not essential to Java’s definition, but neither is it essential to the observation
<div class="">that a downcast (a narrowing reference conversion) is a special case of a cast, and instanceof can be </div>
<div class="">generalised to pair nicely with more casts. I mainly brought it up in response to an email that stated categorically</div>
<div class="">that no subtyping relationship exists among primitives as a way of showing that, as Brian then articulated </div>
<div class="">better, that the way the language actually works is not necessarily equivalent with mental models of the language </div>
<div class="">that each of us forms in our minds through interaction with the language (and those models are likely different </div>
<div class="">from one person to another).</div>
<div class=""><br class="">
</div>
<div class="">There are other casts than downcasts, and the decision that instanceof is only related to downcasts but not </div>
<div class="">other casts is just as arbitrary as the decision that it’s related to all casts, and both of them are consistent with</div>
<div class="">the existing spec.</div>
<div class=""><br class="">
</div>
<div class="">Ultimately, the question of how instanceof is to be generalised — to which casts it relates — has different</div>
<div class="">personal aesthetic answers, none of them philosophically right or wrong. But that’s not what drives the</div>
<div class="">language decision. Rather, what drives the decision is which generalisation is most useful. Examples</div>
<div class="">that show why the proposed generalisation is useful will follow.</div>
<div class=""><br class="">
</div>
<div class="">As to your point about not all ints being valid floats (regardless of the subtyping relationship in §4.10.1),</div>
<div class="">note that while it is a compile-time error to write `x instanceof String` when the compile-time type of `x`</div>
<div class="">is `Integer`, it is not a compile-time error to write `x instanceof List` if the type of `x` is `Serializable`, or</div>
<div class="">`x instanceof Serializable` when the type of `x` is `List`, even though not all Lists are Serializable and </div>
<div class="">not all Serializables are Lists. As an approximation, we allow instanceof when there can be any</div>
<div class="">intersection of instances between the type of the expression and the pattern.</div>
<div class=""><br class="">
</div>
<div class="">The generalisation of instanceof to pair with more kinds of casts is consistent with the language as well</div>
<div class="">as with the mental models some of us have of it, and the relevant discussion about the feature isn’t </div>
<div class="">about how it feels but what problems it solves (and/or creates).</div>
<div class=""><br class="">
</div>
<div class="">— Ron<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 10 Feb 2023, at 23:20, Robbe Pincket <<a href="mailto:robbepincket@live.be" class="">robbepincket@live.be</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; 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;">
<div class="">
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><span lang="EN-US" class="">This email was initially sent to amber-spec-observers by accident.<o:p class=""></o:p></span></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><span lang="EN-US" class=""><o:p class=""> </o:p></span></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Hi all</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">I've long considered whether I should voice my opinion on</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">the matter or not, given that I felt like some of my</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">arguments had already been made before.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Spoiler, I'm not a fan of `instanceof byte`.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Every time I write this email, I recheck something in</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">the archives to check something and realize that someone</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">already said *something* about one of the arguments I'm</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">trying to make. So here is just a list of things that</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">have come up that I still felt like mentioning something.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">---</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">I asked a whether the following would be legal in an earlier email.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">The draft now explains that it is, but I still wonder what this snippet</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">would get desugared to.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">```</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Number num = ...;</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">if (num instanceof byte b) {</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">    ...</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">}</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">```</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">---</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">On 2023-01-27 at 12:01 UTC, Ron Pressler wrote</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">(as reaction to an email by Remi Forax on 2023-01-27 at 11:06 UTC):</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">><i class=""> §4.10.1 of the JLS has stated that a subtyping relationship among primitives exists (byte <: short <: int <: long, float <: double) since at least Java 1.6 (possibly earlier).<o:p class=""></o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">><i class=""><o:p class=""> </o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">>><i class=""> By example, you can not override a method that returns a double with a method that returns an int because overriding is about subtyping relationship, not conversion (for the history, Neal Gafter had implemented method overriding that allow primitive conversions in javac in one of the early versions of Java 1.5 but to it was decided to not go that path).<o:p class=""></o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">><i class=""><o:p class=""> </o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">><i class=""> The rules for return types in overridden methods (§8.4.8.3) doesn’t refer to subtyping but to "return-type-substitutability” (§8.4.5) which, in turn, also doesn’t talk about subtyping but only about subtyping *in the case that the return type is a reference type*. So even though there is a subtyping relationship between int and long (though not between int and double), they are not return-type-substitutable, which places additional constraints beyond mere subtyping.<o:p class=""></o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">It seems you missed a single line in the spec. `long` is also a</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">direct subtype of `float` so `int` is in fact a subtype of `double`</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">>><i class=""> I agree that revisiting concepts is something we have to do, but breaking the difference between subtyping and primitive conversions for a corner case, in isolation, is a decision hard to understand.<o:p class=""></o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">><i class=""><o:p class=""> </o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">><i class=""> First, a subtyping relationship for primitives does exist, though it doesn’t cover all of the possible conversions.<o:p class=""></o:p></i></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Second, nothing is broken, shattered, or wrecked even in cases subtyping doesn’t exist, because the draft doesn’t propose to *change* the meaning of patterns, but rather to generalise them. I.e. its insight is that subtyping is a special case of assignment conversion, and so patterns are “really” about the validity of such conversions rather than about the specific case of subtyping.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">I had a quick look over the spec and I couldn't find any place that</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">actually used the subtyping relation between primitives. The case</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">you seem to be claiming widening primitive conversion matches 100%</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">with the rules about subtyping, it actually doesn't use subtyping</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">at all and just lists all possible conversions.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">A interesting question that could be asked is:</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">"What does subtype/supertype mean". Although it is used in the spec</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">it isn't really explain apart from being a reflexive and transitive</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">relationship between types.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">---</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">A few of the emails argue that we should see `instanceof` as a</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">safe cast check, not like an "inheritance" check or</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">a "reference subtype" check. If the operator was called</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">`canbesafelyconvertedto` I might agree, but thats not the case.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">It feels very weird (to me) to not have an operator called</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">`instanceof`, not check whether the lhs is an instance of the</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">rhs.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Some of you might be thinking: "I guess you want to argue that</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">you can't have instances of an `byte` so `instanceof byte`</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">doesn't make sense", which isn't true. Valhalla discussions</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">have agreed that `byte` has instances. It has 256 different ones:</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">the values from -128 to 127.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Now this might seem I'm arguing in favour of `instanceof byte`.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">And on it's own that might look correct, but then you have</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">justify the fact that the integer values/instances between -128</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">and 127 and the byte values/instances are the same. In my mind</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">the way this would be justified is by using the fact that `byte`</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">is a subtype of `int` which is why it's set of instances is a</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">subset of the instances of `int`. But this raises the issue</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">that the set of instances of `int` can't be a subset of the set</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">of instances of `float` cause not all ints are valid floats.</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">---</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">I feel like I spend too much time writing, rewriting and deleting this</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">email so this is it (for now).</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">TL; DR: I don't like `instanceof byte` and JLS chapter 4.10.1</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class=""><o:p class=""> </o:p></pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Kind regards</pre>
<pre style="margin: 0cm 0cm 0.0001pt; font-size: 10pt; font-family: "Courier New";" class="">Robbe Pincket</pre>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>