I want to define a function which uses a type with multiple bounds, where one of the bounds is another type parameter. For example:
<A, R extends A & SomeInterface> R doSomething(...);
It seems (according to Intellij IDEA) this is not allowed, nor is any type with multiple bounds, where any of those bounds is a type parameter. So these are illegal:
<A, R extends A & SomeInterface> R doSomething(...);
<A, B, R extends A & B> R doSomething(...);
But these are legal:
<R extends SomeType & SomeInterface> R doSomething(...);
<A, R extends A> R doSomething(...);
The case of extending a type parameter and an interface is prohibited, but replacing the type parameter with a literal type (class, enum, or interface) is allowed. I would understand if it was not allowed to have a type parameter as bounds at all, but this is not the case. Is there something I'm missing?
In case this is an xy problem, the precise issue I am trying to solve is this:
public interface Functor<A, Self extends Functor<?, Self>>
{
<B, SelfB extends Self & Functor<B, Self>> SelfB map(Function<A, B> f);
}
The above declaration, if it were legal, seems to provide sufficient constraints to solve this problem; ensuring that the return type is a functor of the same type, with B as its data parameter. This is a case of extending an interface and some other type determined by a type parameter.