Java Generics Class Type Parameter Inference
- by Pindatjuh
Given the interface:
public interface BasedOnOther<T, U extends BasedList<T>> {
public T getOther();
public void staticStatisfied(final U list);
}
The BasedOnOther<T, U extends BasedList<T>> looks very ugly in my use-cases. It is because the T type parameter is already defined in the BasedList<T> part, so the "uglyness" comes from that T needs to be typed twice.
Problem: is it possible to let the Java compiler infer the generic T type from BasedList<T> in a generic class/interface definition?
Ultimately, I'd like to use the interface like:
class X extends BasedOnOther<BasedList<SomeType>> {
public SomeType getOther() { ... }
public void staticStatisfied(final BasedList<SomeType> list) { ... }
}
Instead:
class X extends BasedOnOther<SomeType, BasedList<SomeType>> {
public SomeType getOther() { ... }
public void staticStatisfied(final BasedList<SomeType> list) { ... }
}