Java Reference
In-Depth Information
It is worth noting that a constraint of the form A = F is never part of the initial con-
straints. However, it can arise as the algorithm recurses. We see this occur in the run-
ning example below, when the constraint A << F relates two parameterized types, as
in G < V > << G < U > .
A constraint of the form A >> F also arises when the algorithm recurses, due to the
contravariant subtyping rules associated with lower-bounded wildcards (those of the
form G <? super X > ).
It might be tempting to consider A >> F as being the same as F << A , but the problem
of inference is not symmetric. We need to remember which participant in the relation
includes a type to be inferred.
These constraints are then reduced to a set of simpler constraints of the forms T :> X , T =
X , or T <: X , where T is a type parameter of the method. This reduction is achieved by the
procedure given below.
It may be that the initial constraints are unsatisfiable; we say that inference is over-
constrained . In that case, we do not necessarily derive unsatisfiable constraints on the
type parameters. Instead, we may infer type arguments for the invocation, but once
we substitute the type arguments for the type parameters, the applicability test may
fail because the actual argument types are not acceptable given the substituted formal
parameter types.
An alternative strategy would be to have type inference itself fail in such cases. A
Java compiler may choose to do so, provided the effect is equivalent to that specified
here.
Given a constraint of the form A << F , A = F , or A >> F :
If F does not involve a type parameter T j then no constraint is implied on T j .
Otherwise, F involves a type parameter T j , and there are four cases to consider.
1. If A is the type of null , no constraint is implied on T j .
2. Otherwise, if the constraint has the form A << F :
• If A is a primitive type, then A is converted to a reference type U via boxing
conversion and this algorithm is applied recursively to the constraint U << F .
• Otherwise, if F = T j , then the constraint T j :> A is implied.
• If F = U [] , where the type U involves T j , then if A is an array type V [] , or a type
variable with an upper bound that is an array type V [] , where V is a reference
type, this algorithm is applied recursively to the constraint V << U .
Search WWH ::




Custom Search