Consider the following program:
import java.util.*;
class cast {
Iterable<? extends Number> x1 = null;
Collection<? extends Number> x2 = (Collection<? extends Number>)x1; //javac generates a warning here
}
The JLS seems to mandate a warning (JLS 5.5) for a cast that can be considered 'statically-safe':
"A cast from a type S to a parameterized type (��4.5) T is unchecked unless at least one of the following conditions hold:
1) S <: T.
2) All of the type arguments (��4.5.1) of T are unbounded wildcards.
3) T <: S and S has no subtype XT , such that the erasures (��4.6) of X and T are the same. "
Here we have that:
1) S (Iterable<? extends Number>) is *not* a subtype of T (Collection<? extends Number>)
2) T's type argument (? extends Number) is *not* an unbounded wildcard
3) There exist an *infinite) set of subtypes of S of the kind J = Collection<? extends K>, with K <: Number, such that |J| == |T|
For the above reason it seems like the described cast should raise a warning. Is there any way to fix the JLS so to make this warning disappear?