<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body><div style="font-family: sans-serif;"><div class="markdown" style="white-space: normal;">
<p dir="auto">On 9 Sep 2022, at 11:07, Brian Goetz wrote:</p>
</div><div class="plaintext" style="white-space: normal;"><blockquote style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;"><p dir="auto">… Regardless, a better way to think about `instanceof` is that it is the precondition for "would a cast to this type be safe and useful."  In the world where we restrict to reference types, the two notions coincide.</p>
</blockquote></div>
<div class="markdown" style="white-space: normal;">
<p dir="auto">And, in the future world where every value (except possibly <code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;">null</code>) is an <em>instance</em>, the two notions will coincide again, without the restriction to reference types.  We are taking reasonable incremental steps toward that world here, IMO.</p>
</div><div class="plaintext" style="white-space: normal;"><blockquote style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;"><p dir="auto">But the safe-cast-precondition is clearly more general (this is like the difference between defining the function 2^n on Z, vs on R or C; of course they have to agree at the integers, but the continuous exponential function is far more useful than the discrete one.)  Moreover, the general mental model is just as simple: how do you know a cast is safe?  Ask instanceof.  What does safe mean?  No error or material loss of precision.</p>
</blockquote></div>
<div class="markdown" style="white-space: normal;">
<p dir="auto">And (to pile on a bit here), the casts you are speaking of here, Brian, <em>are the casts we have in Java</em>, not some idealized or restricted or cleaned up cast.  So we have to deal with the oddities of primitive value conversion.</p>
<p dir="auto">The payoff from dealing with this is that the meaning of patterns is derived systematically from the meaning of casts (and other conversions).  That is hugely desirable, because it means a very complex new feature is firmly anchored to existing features.  Getting this kind of thing right preserves and extends Java’s role as a world-class programming language.</p>
</div><div class="plaintext" style="white-space: normal;"><blockquote style="margin: 0 0 5px; padding-left: 5px; border-left: 2px solid #777777; color: #777777;"><p dir="auto">A more reasonable way to state this objection would be: "most users believe that `instanceof` is purely about subtyping, and it will take some work to bring them around to a more general interpretation, how are we going to do that?"</p>
</blockquote></div>
<div class="markdown" style="white-space: normal;">
<p dir="auto">This is subjective and esthetic, but I think two thoughts help here (with teaching and rationale):  First, everything (except <code style="margin: 0; padding: 0 0.4em; border-radius: 3px; background-color: #F7F7F7;">null</code>) is an instance, or will eventually be.  Second, subtyping in Java includes the murky rules for primitive typing.</p>
<p dir="auto">Those specific rules more or less systematically determine how casts work.  They should also systematically determine (in the same way) how patterns work.  After all, casts and patterns are (and very much should be!) mirror image counterparts of each other, or dance partners holding hands.</p>
<p dir="auto">(I visualize such things as boxes on the whiteboard with reversible arrows between them.  You could say “category” if you like.  Brian likes to say “dual”, and I took linear algebra too, but I doubt most folks took the trouble in that class to be curious about exactly what a “dual space” really is all about.)</p>
<p dir="auto">Rather than extending the language we wish we had, we are extending the one we <em>do</em> have, and that means aligning even the murky parts of casts with pattern behavior.</p>
<p dir="auto">In the end, I don’t think it’s very murky at all in practice, except of course for the outraged theoretical purist (who lives in each of us).  There is certainly <em>no new murk</em>.  IMO what Brian is showing works out surprisingly well, so kudos to him for following his nose to a design with liveable details. This success also IMO demonstrates the foresight of the original authors and current maintainers of the spec, even in the “murky” parts of primitive value conversions.</p>
<p dir="auto">— John</p>

</div></div></body>

</html>