Is V? what we need ?
Brian Goetz
brian.goetz at oracle.com
Wed May 1 19:34:22 UTC 2019
> However, I think my more general point remains - the fact that the VM
> has the flexibility to treat V and V? as, fundamentally, the same
> thing shouldn't necessarily drive the conclusion that, _at the
> language level_ there should be a subtyping relationship between V and
> V?.
>
> From a pedagogical perspective, whether we like it or not, users are
> immediately going to think in terms of box/unbox, so I think it'd be
> surprising if the new inline classes and their projections had rules
> radically different from those which apply to the int/Integer case.
I agree that users are going to first reach for the box/unbox
intuition. So let's talk about what's going to happen there.
Let's assume that `V` translates to `QV` and `V?` translates to `LV`;
let's compare the language we get under a subtyping relationship or a
boxing conversion. (Either way, we get V? <: Object.)
The box intuition says you can lock on a box (though we discourage it.)
But locking on a V? will result in an exception. (Same for wait/notify.)
The box intuition says that == on a box is an identity comparison. But a
V? has no identity (it did in Q-world, not in L-world.)
The box intuition says you can create a weak reference of a box. But you
cannot for a value.
So there are multiple ways in which the box intuition _already_ leads
users down the wrong road, regardless of whether it is subtyping or not.
One big difference between whether we convert V to V? via subsumption or
via a boxing conversion is how overload selection is done. But, if we
have methods m(Object) and m(V?), and it is invoked with a v, in the
subtyping case, we'll select m(Object), and in the conversion case,
we'll select m(V?). But it's not clear that this is what we want -- I
would think to most people, V? feels "more specific" than Object. The
rules regarding "try first without boxing" exist solely for
compatibility; they are not necessarily the rules we would have wanted
had we started with autoboxing in 1.0.
If we define the relationship between V and V? as boxing, then equality
comparisons between V and V? similarly have to go through boxing. This
is doable, but under subtyping, it is simpler -- do they refer to the
same value.
Overall, I'm not really seeing much of value (that joke never gets old)
for the "it's a boxing conversion" route; it's more complexity in the
language, but the boxing intuition is not really serving the user very
well in the first place.
More information about the valhalla-spec-experts
mailing list