Java Generics Class Type Parameter Inference
Posted
by Pindatjuh
on Stack Overflow
See other posts from Stack Overflow
or by Pindatjuh
Published on 2010-06-16T18:09:47Z
Indexed on
2010/06/16
18:12 UTC
Read the original article
Hit count: 302
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) { ... }
}
© Stack Overflow or respective owner