Search code examples
javascalagenericstype-parametertraits

Does Java allow type parameters for type parameters?


In Scala you can do such things:

trait A[T]

trait B[C[_] <: A[_]] {
    def apply[T](entity: C[T]): T
}

The Java analog would look something like this:

interface A<T>

interface B<C<?> extends A> {
   <T> T apply(entity: C<T>): T
}

You can use B with any type that extends A and use this sub-type's type parameter as an output type of the method.

But it doesn't compile, how would you do it in Java?


Solution

  • No, you unfortunately can't do this. Java does not have higher kinded types.