Eddie Jamsession
Eddie Jamsession

Reputation: 1006

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?

Upvotes: 3

Views: 135

Answers (1)

drexin
drexin

Reputation: 24403

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

Upvotes: 6

Related Questions