Java Generics Class Parameter Type 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:22 UTC
Read the original article Hit count: 275

Filed under:
|
|

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 implements BasedOnOther<BasedList<SomeType>> {
    public SomeType getOther() { ... }
    public void staticStatisfied(final BasedList<SomeType> list) { ... }
} // Does not compile, due to invalid parameter count.

Instead:

class X implements BasedOnOther<SomeType, BasedList<SomeType>> {
    public SomeType getOther() { ... }
    public void staticStatisfied(final BasedList<SomeType> list) { ... }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about generics