df
07/25/2023, 9:30 AMAdam S
07/25/2023, 9:40 AMWout Werkman
07/25/2023, 9:58 AMFoo
looks like this:
class Foo {
fun cancel(userId: String?, event: Cancel, options: EventOptions? = null, extra: MiddlewareExtra? = null) {}
fun ship(userId: String?, event: Ship, options: EventOptions? = null, extra: MiddlewareExtra? = null) {}
val map = mapOf<KClass<out Event<*>>, (Foo, String?, Event<*>, EventOptions?, MiddlewareExtra?) -> Unit>(
Cancel::class to Foo::cancel,
Ship::class to Foo::ship
)
}
You need to understand that function parameters are in
when used as generics. (Cancel) -> Unit
is not a subtype of (Event<*>) -> Unit
.
Imagine the following scenario.
val ship: Ship = TODO()
val cancelProcessor: (Cancel) -> Unit = TODO()
val someProcessor: (Event<*>) -> Unit = cancelProcessor as (Event<*>) -> Unit
someProcessor(ship) // Will throw runtime exception
Now you can hack around this by just adding casts as follows:
val map = mapOf<KClass<out Event<*>>, (String?, Event<*>, EventOptions?, MiddlewareExtra?) -> Unit>(
Cancel::class to Foo::cancel as (String?, Event<*>, EventOptions?, MiddlewareExtra?) -> Unit,
Ship::class to Foo::ship as (String?, Event<*>, EventOptions?, MiddlewareExtra?) -> Unit,
)
Which will trick the compiler into allowing this.df
07/26/2023, 9:58 AMWout Werkman
07/26/2023, 1:28 PMWout Werkman
07/26/2023, 1:29 PM