If I have a class like the following:
class SimpleClass {
var test: Int = 1
}
I can do something like this:
inline fun <reified T : SimpleClass> myFunction1(): T {
//Do something with T
}
But if I have a class with generics:
class ComplexClass<T> {
var test: T? = null
}
How can I do the same?
The following doesn't work:
inline fun <reified T : ComplexClass<C>> myFunction2(): T {
//Do something with T
//Do something with C
}
How can I do it?
Here is a more complex example, if needed.
Let's take a look at your example
To perform something with the type
C
which is used to createContainer<C>
and perform something with the result type which you represent asT : Container<C>
you only need to know theC
. Sincereified
can only be used to keep the type if it is known during the compile time at the call site, rewrite your function signature like this.Use it like following
Here is my minimal example from Kotlin Playground