I have an interface parametrized on a type which extends a parametrized type, and that interface contains a method which needs the type parameter of the type being extended. In code, this is what I want:
class Thingy<X> { ... }
interface Foo<P extends Thingy<?>> {
<T> T frobnicate(P<T> a);
}
But that doesn't compile. In order to properly frobnicate
a Thingy
, it's necessary to know the type of Thingy
it is, but also some Foo
s may be specialized for handling only particular kinds of Thingy
s (the exception being that they are required to be able to handle whatever type T
any Thingy
is parametrized on).
(BTW, I've already spotted these questions. The former is about requiring only one generic parameter; I don't care if I need one, two, or five here, so long as there's some solution. The latter has a specific type rather than a wildcard in the class corresponding to my Foo
, so the solution there isn't applicable here, so far as I can see.)
EDIT:
Since the number of subclasses of Thingy
I intend to have is small, I can get the same effect by subclassing Thingy
and creating another interface just for handling those:
interface Foo {
<T> T frobnicate(Thingy<T> a);
}
class SpecialThingy<T> extends Thingy<T> { ... }
interface Bar {
<T> T frobnicate(SpecialThingy<T> a);
}
That's not as elegant as I would have liked, but it will do the job in this case.