Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin and RxJava type inference failed

I am trying to implement retryWhen on my Observable when there is ocurring a timeot, but i have strange errors underlined in IDE Android Studio 3.0

Here is the code:

rxRssiRepository.onRssiUpdate() //returns Observable<RssiEvent>
        .timeout(10, TimeUnit.MILLISECONDS)
        .retryWhen { t: Observable<Throwable> ->
            t.flatMap { error: Throwable ->
                if (error is TimeoutException) {
                    stopLocationUpdates()
                    Log.v("TIMEOUT", "TIMEOUT RSSI EVENTS")
                    Observable.just(Observable.empty())
                } else {
                    Observable.error(error)
                }
            }
        }
        .concatMap { t: RssiEvent ->
            appendRssiEvent(t)
        }
        .publish()

The IDE underlines on red the .flatMap operator and says:

Type inference failed: Not enough information to infer parameter R in

fun flatMap ( mapper: ((t: Throwable) → ObservableSource)! ) : Observable! Please specify it explicitly.

Same for the 'if' operator:

Type inference for control flow expression failed. Please specify its type explicitly

For operator .just:

Type inference failed: Not enough information to infer parameter T in fun just ( item: T! ) : Observable! Please specify it explicitly.

For operator .empty:

Type inference failed: Not enough information to infer parameter T in

fun just ( item: T! ) : Observable! Please specify it explicitly.

And for the .error operator:

Type inference failed: Not enough information to infer parameter T in

fun error ( exception: Throwable! ) : Observable! Please specify it explicitly.

How to fix this?

I mean, this code was also here in Java: How to add a timeout to detect that an Observable didn't emit for a while

But when i convert it similiary to Kotlin, it gives me the described situation issue

like image 848
K.Os Avatar asked Nov 20 '17 11:11

K.Os


1 Answers

The compiler can't figure out what type you want to return.

The easiest fix in your case would be to add a type your return type. Something like this Observable.just(Observable.empty<Any>()). Now the compiler will be able to figure out what type to return.

like image 198
jbarat Avatar answered Nov 01 '22 11:11

jbarat