Java Reference
In-Depth Information
This concludes the process of determining constraints on the type parameters
of a method.
Note that this process does not impose any constraints on the type parameters
based on their declared bounds. Once type arguments are inferred, they will be
tested against the declared bounds of the type parameters as part of applicabil-
ity testing.
Note also that type inference does not affect soundness in any way. If the types
inferred are nonsensical, the invocation will yield a type error. The type infer-
ence algorithm should be viewed as a heuristic, designed to perform well in
practice. If it fails to infer the desired result, explicit type paramneters may be
used instead.
Next, for each type variable T j (1 ≤ j n ), the implied equality constraints are resolved as
follows.
For each implied equality constraint T j = U or U = T j :
• If U is not one of the type parameters of the method, then U is the type inferred for
T j . Then all remaining constraints involving T j are rewritten such that T j is replaced
with U . There are necessarily no further equality constraints involving T j , and pro-
cessing continues with the next type parameter, if any.
• Otherwise, if U is T j , then this constraint carries no information and may be dis-
carded.
• Otherwise, the constraint is of the form T j = T k for j k . Then all constraints in-
volving T j are rewritten such that T j is replaced with T k , and processing continues
with the next type variable.
Then, for each remaining type variable T j , the constraints T j :> U are considered. Given that
these constraints are T j :> U 1 ... T j :> U k , the type of T j is inferred as lub( U 1 ... U k ), computed
as follows:
For a type U , we write ST( U ) for the set of supertypes of U , and define the erased supertype
set of U :
EST( U ) = { V | W in ST( U ) and V = | W | } where | W | is the erasure (§ 4.6 ) of W .
The reason for computing the set of erased supertypes is to deal with situations where
a type variable is constrained to be a supertype of several distinct invocations of a
generic type declaration.
Search WWH ::




Custom Search