Java Generics Oddity

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.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.