Unit 27: Type Inference
After this unit, students should:
- be familiar how Java infers missing type arguments
We have seen in the past units the importance of types in preventing run-time errors. Utilizing types properly can help programmers catch type mismatch errors that could have caused a program to fail during run-time, possibly after it is released and shipped.
By including type information everywhere in the code, we make the code explicit in communicating the intention of the programmers to the readers. Although it makes the code more verbose and cluttered -- it is a small price to pay for ensuring the type correctness of the code and reducing the likelihood of bugs as the code complexity increases.
Java, however, allows the programmer to skip some of the type annotations and try to infer the type argument of a generic method and a generic type, through the type inference process.
The basic idea of type inference is simple: Java will looking among the matching types that would lead to successful type checks, and pick the most specific ones.
Diamond Operator
One example of type inference is the diamond operator <>
when we new
an instance of a generic type:
1 |
|
Java can infer that p
should be an instance of Pair<String,Integer>
since the compile-time type of p
is Pair<String,Integer>
. The line above is equivalent to:
1 |
|
Type Inferencing
We have been invoking
1 2 3 4 5 6 7 8 9 10 11 12 |
|
by explicitly passing in the type argument Shape
(also called type witness in the context of type inference).
1 |
|
We could remove the type argument <Shape>
so that we can call contains
just like a non-generic method:
1 |
|
and Java could still infer that S
should be Shape
. The type inference process looks for all possible types that match. In this example, the type of the two parameters must match. Let's consider each individually first:
- An object of type
Shape
is passed as an argument to the parameterobj
. SoS
might beShape
or, if widening type conversion has occurred, one of the other supertypes ofShape
. - An
Array<Circle>
has been passed intoArray<? extends S>
. A widening type conversion occurred here, so we need to find all possibleS
such thatArray<Circle>
<:Array<? extends S>
. This is true only ifS
isCircle
, or another supertype ofCircle
.
Intersecting the two lists, we know that S
could be Shape
or one of its supertypes: GetAreable
and Object
. The most specific type among these is Shape
. So, S
is inferred to be Shape
.
Type inferencing can have unexpected consequences. Let's consider an older version of contains
that we wrote:
1 2 3 4 5 6 7 8 9 10 11 |
|
Recall that we want to prevent nonsensical calls where we are searching for an integer in an array of strings.
1 2 |
|
But, if we write:
1 |
|
The code compiles! Let's go through the type inferencing steps to understand what happened. Again, we have two parameters:
strArray
has the typeString[]
and is passed toT[]
. SoT
must beString
or its superclassObject
. The latter is possible since Java array is covariant.123
is passed as typeT
. The value is treated asInteger
and, therefore,T
must be eitherInteger
or its superclassObject
.
The intersection between the two is the type Object
, which is also the most specific type. So, Java infers T
to be Object
. The code above is equivalent to:
1 |
|
And our version 0.4 of contains
actually is quite fragile and does not work as intended. We were bitten by the fact that the Java array is covariant, again.
Target Typing
The example above performs type inferencing on the parameters of the generic methods. Type inferencing can involve the type of the expression as well. This is known as target typing. Take the following upgraded version of findLargest
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
and we call
1 |
|
We have a few more constraints to check:
- Due to target typing, the returning type of
T
must be a subtype ofShape
(includingShape
) - Due to the bound of the type parameter,
T
must be a subtype ofGetAreable
(includingGetAreable
) Array<Circle>
must be a subtype ofArray<? extends T>
, soT
must be a supertype ofCircle
(includingCircle
)
Intersecting all these possibilities, only two possibilities emerge: Shape
and Circle
. The most specific one is Circle
, so the call above is equivalent to:
1 |
|