I’ve been looking at Java generics a bit, and I found something a
bit weird. Suppose you have a class like this:
class k<T extends Throwable & Comparable> { int doSomething(T arg) { return arg.compareTo(this); } }
First, note that Throwable
doesn’t
implement Comparable
(and in fact multiple constraints
aren’t very useful when one implements another).
What should the generated code look like? According to the
generics paper I have, no cast is required in this situation.
But that can only be correct if the verifier always allows implicit
casts to an interface type to succeed, and then requires the execution
engine to detect incorrect calls.
This seems wrong to me. Instead it seems like the compiler must
insert an explicit cast whenever a value whose type is a type variable
is implicitly cast to a type other than its erasure type.
Be the first to leave a comment. Don’t be shy.