Reputation: 3603
I'm trying to write an extension function for Observable.subscribe which automatically logs errors.
fun <T> Observable<T>.subscribeAndLogE(onNext: Consumer<in T>): Disposable =
subscribe(onNext, ErrorConsumer())
The ErrorConsumer works and presumably logs the error, but subscribeAndLogE
does not accept lambdas like .subscribe()
does.
observer.subscribe {
//works
}
observer.subscribeAndLogE {
//does not work
}
It says:
With that OnboardingScreen being whichever value T
would normally be.
I don't see the original Consumer<in T>
in Observable
doing anything special to accept lambdas. What am I doing wrong here?
Upvotes: 5
Views: 1362
Reputation: 2054
You are passing a parameter of type Consumer
to the function. You need to pass a function for the lambda syntax to work. This would work the way you want to:
fun <T> Observable<T>.subscribeAndLogE(onNext: (it : T) -> Unit): Disposable =
subscribe({ onNext(it) },{ throwable -> Log(throwable) })
and use it like so:
observer.subscribeAndLogE {
//works
}
Upvotes: 5