I’m having something “observable” like this:
class SomethingObservable {
public fun onSomething(listener: (event: Event) -> Unit) {
// adds listener
}
public fun unSomething(listener: (event: Event) -> Unit) {
// removes listener
}
}
And I would like to pass a class member function as the function reference like this:
class SomeoneObserving {
val somethingObservable: SomethingObservable
fun startObserving() {
somethingObservable.onSomething(::handleEvent)
}
fun stopObserving() {
somethingObservable.unSomething(::handleEvent)
}
private fun handleEvent(event: Event) {
...
}
}
But instead I get the compilation errors:
Type mismatch: inferred type is kotlin.reflect.KFunction2<SomeoneObserving, Event, kotlin.Unit> but (Event) -> kotlin.Unit was expected
Left-hand side of a callable reference with a receiver parameter cannot be empty. Please specify the type of the receiver before '::' explicitly
Does anyone know how to accomplish this?
Thanks!
Jørund