Hi! Any ideas why this doesn't compile? Hope it's ...
# announcements
i
Hi! Any ideas why this doesn't compile? Hope it's fine to put rx code here. It's a lang question.
Copy code
fun foo(): Observable<Result<String, String>> =
    Observable.just(Success(""))
        .onErrorResumeNext { t: Throwable -> Failure("") } // Type mismatch: inferred type is Observable<Result.Success<String>!>! but Observable<Result<String, String>> was expected

// where:

sealed class Result<out T, out E> {
    data class Success<out T>(val success: T) : Result<T, Nothing>()
    data class Failure<out E>(val error: E) : Result<Nothing, E>()
}
d
Observable.just(Success(""))
is inferred to be of type
Observable<Result.Success<String>>
. Your
onErrorResumeNext
then tries to put a
Result.Failure<String>
into that, which is not compatible.
You either have to specify the type explicitly when calling
just
or create factory methods for
Success
and
Failure
which return
Result<T, E>
instead of
Result.Success
/
Result.Failure
i
if I try the first:
Copy code
fun foo(): Observable<Result<String, String>> =
    Observable.just<Result<String, String>>(Success(""))
        .onErrorResumeNext { t: Throwable -> Failure("") }
I get a new error on
Failure("")
Copy code
Expected type mismatch: inferred type is Result.Failure<String> but ObservableSource<out Result<String, String>!>! was expected
­čĄö
ok, how do I specify that the later returns also
Result<String, String>
?
d
onErrorResumeNext
expects an observable again, not a plain value
You probably want
onErrorReturn
instead
i
ah, too many things going on at once ­čÖé thanks! It compiles now