Mike Schilling said:
If you're implying that you'd understand it after you read the spec, it must
be a better-written spec than the ones I've seen.
Well, no, but understanding specs is not impossible -- just an acquired
skill. I would have said "yes" until the third edition came out.
Generics, though, complicate the specification even more than the
language.
Here's what I'm seeing here doesn't lead to the conclusion I expected,
though. Perhaps someone can clarify. The expression in question was:
ArrayList<Node>[] arr = new ArrayList[n];
Eclipse and javac both compile this but give a warning about unchecked
conversion.
5.2 (Assignment Conversion) says that we have two options for the
assignment conversion. We should apply (among other irrelevant things)
widening reference conversion to convert the type ArrayList[] to
ArrayList<Node>[] first. If that's not possible, we should apply
unchecked conversion to do so.
5.1.5 (Widening Reference Conversions) says that widening primitive
conversion is only possible if the type ArrayList[] is a subtype of
ArrayList<Node>[].
4.10.3 (Subtyping among Array Types) says that ArrayList[] is a subtype
of ArrayList<Node>[] if and only if ArrayList is a subtype of
ArrayList[].
4.10.2 (Subtyping among Class and Interface Types) says that
ArrayList<Node> is a subtype of ArrayList, but NOT vice versa. So we've
failed.
Back to 5.2 (Assignment Conversion), the next option is to apply
unchecked conversion from ArrayList[] to ArrayList<Node>[]. This only
applies if ArrayList[] is a raw type.
4.8 (Raw Types) makes ArrayList[] NOT a raw type, even though its
component type is a raw type. Therefore we can't apply unchecked
conversion.
So it seems that the code above should actually fail to compile. Yet it
does compile. To make that correct, it would seem that it would be
necessary to expand the definition of unchecked conversion in 5.1.9 to
include the rule that if an unchecked conversion exists from T to S,
then an unchecked conversion exists from T[] to S[], and then to update
text in sections like 5.2 to remove the requirement (which is
unnecessary anyway) that unchecked conversion is only applied to raw
types.