Higher Kinded Types A: * -> *

Hello

It would be very useful to have higher kinded types in Kotlin.
I have the following interfaces:

interface A<T> {}
interface B<T> : A<T> {}

In this example A<T> and B<T> are first order types (*).
But A and B are “higher” types (* → *) in that they need a parameter T to make a “normal”
fist order type.

Support for higher order types would be useful in the following situation.
I want to have a function, that maps an A<T> to A<T> and B<T> to B<T>:

fun <X: A,T> f(x: X<T>): X<T>

This is currently not possible within the Kotlin type system.

Type arguments are not allowed for type parameters

A workaround would be to encode the output type in the Interface:

interface A<X: A<X,T>,T> {}
interface B<T> : A<B<T>,T> {}
interface C<T> : A<C<T>,T> {}


fun <X: A<X,T>,T> f(x: X): X

But this is overly complicated and it does not allow me to write functions for
interfaces that I import from libraries that I don’t control.

2 Likes

How does it work in the case X is not parameterized or its parameter is a different thing than T of A? Is T here always referencing a parameter type of A?