generics - Where does the Java spec say List<T> assigns to List<? super T>? -
assume class b inherits class a. following legal java:
list<a> x; list<? super b> y = x; in terms of specification, means list<a> assignsto list<? super b>. however, having trouble finding part of spec says legal. in particular, believe should have subtype relation
list<a> <: list<? super b> but section 4.10 of java 8 spec defines subtype relation transitive closure of direct supertype relation s >1 t, , defines direct supertype relation in terms of finite function computes set of supertypes of t. there no bounded function on input list<a> can produce list<? super b> since there might arbitrary number of bs inherit a, spec's subtype definition seems break down super wildcards. section 4.10.2 on "subtyping among class , interface types" mention wildcards, handles other direction wildcard appears in potential subtype (this direction fits computed direct supertype mechanism).
question: part of spec says above code legal?
the motivation compiler code, it's not enough understand why legal intuitively or come algorithm handles it. since general subtyping problem in java undecidable, handle same cases spec, , therefore want part of spec handles case.
list<? super b> defined supertype of list<a> §4.10.2. subtyping among class , interface types:
the direct supertypes of parameterized type
c<t1,...,tn>,ti(1 ≤ ≤ n) type, of following:
d<u1 θ,...,uk θ>,d<u1,...,uk>direct supertype ofc<t1,...,tn>,θsubstitution[f1:=t1,...,fn:=tn].
c<s1,...,sn>,sicontainsti(1 ≤ ≤ n) (§4.5.1).
let c<t1,...,tn> = list<a> , c<s1,...,sn> = list<? super b>. according second bullet, list<? super b> supertype of list<a> if ? super b contains a.
the contains relation defined in §4.5.1. type arguments , wildcards:
a type argument
t1said contain type argumentt2, writtent2 <= t1, if set of types denotedt2provably subset of set of types denotedt1under reflexive , transitive closure of following rules (where<:denotes subtyping (§4.10)):
? extends t <= ? extends sift <: s
? super t <= ? super sifs <: t
t <= t
t <= ? extends t
t <= ? super t
by second bullet, can see ? super b contains ? super a. last bullet, see ? super a contains a. transitively, therefore know ? super b contains a.
Comments
Post a Comment