https://kotlinlang.org logo
#rx
Title
# rx
a

adams2

06/21/2019, 4:34 PM
how do I easily use kotlin method references with rxjava subscribes?
for example somehow this works
Copy code
myObservable.subscribe(myObject::myMethod, ::myErrorMethod)
yet somehow this does not
myObservable.subscribe(myObject::myMethod, Consumer<Throwable> { ... })
and I am forced to do something awkward like this
myObservable.subscribe(Consumer { myObject.myMethod(it) }, Consumer<Throwable> { ... })
g

gildor

06/21/2019, 4:37 PM
This is limitation of current SAM and type inference in Kotlin, many of those will be fixed with new type inference system, for now you can use RxKotlin library, which provides Kotlin friendly extension functions to solve such issues (for this particular case they have nice subscribeBy extension, where you can just pass Kotlin lambdas instead of Java interface with SAM)
👁️ 1
l

liminal

06/21/2019, 6:32 PM
Thanks all
2 Views