Czar
02/06/2020, 11:27 AMsuspend inline fun <reified T : Event> Client.addHandler(noinline action: suspend (T) -> Unit)
I have a handler interface
interface EventHandler<T : Event> {
suspend fun handle(event: T)
}
And I have a list of handlers which is injected into my application: val handlers: List<EventHandler<*>>
Each handler is defined as MyHandler : EventHandler<MyEvent>
I need to somehow add these handlers to the client: handlers.forEach { client.addHandler(it::handle) }
I'm getting
Cannot use 'Nothing' as reified type parameterat the
.addHandler
What can I do to work around it?Dico
02/06/2020, 12:04 PMDico
02/06/2020, 12:05 PMDico
02/06/2020, 12:06 PMCzar
02/06/2020, 12:12 PMoverride suspend fun registerTo(client: Client) {
client.addHandler(action = this::handle)
}
But I guess, it's the lesser of the two evilsCzar
02/06/2020, 12:19 PMinterface EventHandler {
suspend fun registerTo(client: Client)
}
and implementations will become:
class MyEventHandler : EventHandler {
override suspend fun registerTo(client: Client) {
client.addHandler<MyEvent> {
// stuff
}
}
}