In kotlin, I'm trying to create a dispatch table:
class Foo {
fun handleEvent(bytes:ByteArray) {
// do something fun with the bytes
}
}
class Bar {
fun handleEvent(bytes:ByteArray) {
// do something fun with the bytes
}
}
foo = Foo()
bar = Bar()
val eventHandlers:HashMap<RemoteEvent, (bytes:ByteArray)->Unit> = hashMapOf(
0x01 to foo.handleEvent,
0x02 to bar.handleEvent)
Kotlin doesn't seem to like this, it complains in multiple ways, but the relevant one seems to be be function invocation expected
.
I can fix this by wrapping those in closures:
val eventHandlers:HashMap<RemoteEvent, (bytes:ByteArray)->Unit> = hashMapOf(
0x01 to { bytes -> foo.handleEvent(bytes) },
0x02 to { bytes -> bar.handleEvent(bytes) })
Is there no other way? Why do I have to the method signatures, which are correct, in closures which are the same? Are closures and methods not on the same footing in Kotlin?