Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 782
Eddie Jamsession Avatar asked Apr 08 '26 22:04

Eddie Jamsession


1 Answers

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

like image 79
drexin Avatar answered Apr 11 '26 12:04

drexin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!