jean
05/05/2021, 5:44 AMinterface Executor<T, U> {
fun isExecutable(): (T) -> Boolean
fun execute(): suspend (U) -> Unit
}
class MyClass<T, U> {
val executors = mutableMapOf<String, Executor<T, U>>()
fun register(eventName: String, executor: Executor<T, U>) {
executors[eventName] = executor
}
inline fun <reified V : U> register(executor: Executor<T, V>) {
val eventName = V::class.toString()
register(eventName, executor)
}
}
I get the following error from the IDE Type mismatch. Required: Executor<T, U>, Found: Executor<T, V>
U
is a sealed class and V
a subtype of that class. I did read the page about generic and “PECS” in the doc, but I still haven’t figured it out. Anyone knows what I need to change?ephemient
05/05/2021, 6:35 AMephemient
05/05/2021, 6:39 AMobject A : Executor<Any?, Int> {
override fun isExecutable() = { true }
override fun execute() = suspend {
assert(it is Int)
}
}
val obj = MyClass<Any?, Any?>()
obj.register(A) // you want this function?
obj.executors.values.first().execute()("string") // this is allowed by the interface but cannot work
Mikhail Buzuverov
05/05/2021, 7:50 AMin
but not out