mk
12/06/2016, 1:23 PMIt seems, what I need is a special function type with the signature. I can write that interface down myself, but that I wouldn't be able to use lambda syntax (without writing down a factory function).operator fun <T> invoke(p1: T): R<T>
Looks like what you want are higher kinded types, which Kotlin does not support. There are ways to approximate them with some level of type safety (at least without resorting tohowever isn't a proper typeR<T>
Any
) but it's a little involved. What you can do is wrapping instances of type constructors like your Future in a "kinded context" (an interface basically) by flattening your types. R<T>
becomes Kind1<R, T>
, R<S, T>
becomes Kind2<R, S, T>
and so forth. The trick is then to "instantiate" R
by using an inner class:
class FutureKind<T>(val future: Future<T>) : Kind1<FutureKind.R, T> {
class R
}
any code that acts on type constructors can then be expressed by using an appropriate instantiation of Kind1
. You can use type aliases in Kotlin 1.1 to make this a bit more readable. There will still be occasions where you'll have to downcast a Kind1
though to access the wrapped value, but you can make this relatively safe by having an extension method that performs the cast on a configuration of the specific types. This cast would only fail if you had 2 different implementations for future, which is quite unlikely. But yeah, it's a lot of boilerplate, might not be worth it in your case...