I\'m not sure what the technical term for this is, but consider an interface:
public interface SomeInterface {
public T doSomething();
}
See if this suit your need:
public interface SomeRelatedInterface<T> {
public <D extends SomeInterface<T>> T doSomethingRelated(D relative);
}
As the type parameters are erased at compile time, IMHO you unfortunately cannot achieve what you want without specifying T as the second type parameter, just as you did in your first example.
public interface SomeRelatedInterface<T> {
T doSomethingRelated(SomeInterface<T> relative);
}
Well, I started a bounty on this question, and didn't know that SO's behavior was to award someone the answer (congratulations Daniel), I thought the rep would go unrewarded and I would lose it. Oh well.
Anyway, I finally have my answer. From here:
Unfortunately, for the purposes of backwards compatibility, new Map() indicates a raw type, and therefore cannot be used for type inference.
So basically when creating a class and passing in the type parameter, type inference was disabled to leave room for the raw type. So in my case there could be some type inference but that would be a question of having a more complex different kind of type inference to handle this case, which wasn't done.
"At this point what I am looking for on this question is the reason that the language requires this duplication"
Well, the language requires that you define 2 type parameters in your example because there are, um, 2 type parameters in the problem you describe: you wish a method to be variable in both the type T
and also in the implementation of SomeInterface
.
These are orthogonal considerations and hence you need more than one type parameter to represent them.
Type parameters do not of course need to be defined on a class/interface; they can be defined on a method. J-16 SDiZ's answer allows your related class/interface to have one type parameter only. The second type parameter is then declared only where it is needed, on the doSomethingRelated
method