Java Reference
In-Depth Information
equate one type parameter with another, and only when the entire set of constraints on
all type variables is resolved will we have a solution.
A supertype constraint
T
:>
X
implies that the solution is one of supertypes of
X
. Given
several such constraints on
T
, we can intersect the sets of supertypes implied by each
of the constraints, since the type parameter must be a member of all of them. We can
then choose the most specific type that is in the intersection.
Computing the intersection is more complicated than one might first realize. Given
that a type parameter is constrained to be a supertype of two distinct invocations of
a generic type, say
List<Object>
and
List<String>
, the naive intersection operation might
yield
Object
. However, a more sophisticated analysis yields a set containing
List<?>
.
Similarly, if a type parameter
T
is constrained to be a supertype of two unrelated in-
terfaces
I
and
J
, we might infer
T
must be
Object
, or we might obtain a tighter bound
of
I
&
J
. These issues are discussed in more detail later in this section.
We use the following notational conventions in this section:
• Type expressions are represented using the letters
A
,
F
,
U
,
V
, and
W
. The letter
A
is
only used to denote the type of an actual argument, and
F
is only used to denote
the type of a formal parameter.
• Type parameters are represented using the letters
S
and
T
• Arguments to parameterized types are represented using the letters
X
and
Y
.
• Generic type declarations are represented using the letters
G
and
H
.
Inference begins with a set of initial constraints of the form
A
<<
F
,
A
=
F
, or
A
>>
F
,
where
U
<<
V
indicates that type
U
is convertible to type
V
by method invocation conver-
conversion.
In a simpler world, the constraints could be of the form
A
<:
F
- simply requiring that
the actual argument types be subtypes of the formal ones. However, reality is more
involved. As discussed earlier, method applicability testing consists of up to three
phases; this is required for compatibility reasons. Each phase imposes slightly differ-
indeed subtyping constraints. If a method is applicable by method invocation conver-
al type by method invocation conversion. The situation is similar for the third phase