So, I understand that the following doesn't work, but why doesn't it work?
interface Adapter<E> {}
class Adaptulator<I> {
<E, A extends I & Adapter<E>> void add(Class<E> extl, Class<A> intl) {
addAdapterFactory(new AdapterFactory<E, A>(extl, intl));
}
}
The add()
method gives me a compile error, "Cannot specify any additional bound Adapter<E> when first bound is a type parameter" (in Eclipse), or "Type parameter cannot be followed by other bounds" (in IDEA), take your pick.
Clearly you're just Not Allowed to use the type parameter I
there, before the &
, and that's that. (And before you ask, it doesn't work if you switch 'em, because there's no guarantee that I
isn't a concrete class.) But why not? I've looked through Angelika Langer's FAQ and can't find an answer.
Generally when some generics limitation seems arbitrary, it's because you've created a situation where the type system can't actually enforce correctness. But I don't see what case would break what I'm trying to do here. I'd say maybe it has something to do with method dispatch after type erasure, but there's only one add()
method, so it's not like there's any ambiguity...
Can someone demonstrate the problem for me?
I'm also not sure why the restriction is there. You could try sending a friendly e-mail to the designers of Java 5 Generics (chiefly Gilad Bracha and Neal Gafter).
My guess is that they wanted to support only an absolute minimum of intersection types (which is what multiple bounds essentially are), to make the language no more complex than needed. An intersection cannot be used as a type annotation; a programmer can only express an intersection when it appears as the upper bound of a type variable.
And why was this case even supported? The answer is that multiple bounds allow you to control the erasure, which allows to maintain binary compatibility when generifying existing classes. As explained in section 17.4 of the book by Naftalin and Wadler, a max
method would logically have the following signature:
public static <T extends Comparable<? super T>> T max(Collection<? extends T> coll)
However, this erases to:
public static Comparable max(Collection coll)
Which does not match the historical signature of max
, and causes old clients to break.
With multiple bounds, only the left-most bound is considered for the erasure, so if max
is given the following signature:
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
Then the erasure of its signature becomes:
public static Object max(Collection coll)
Which is equal to the signature of max
before Generics.
It seems plausible that the Java designers only cared about this simple case and restricted other (more advanced) uses of intersection types because they were just unsure of the complexity that it might bring. So the reason for this design decision does not need to be a possible safety problem (as the question suggests).
More discussion on intersection types and restrictions of generics in an upcoming OOPSLA paper.