更新到RxJava 2.x.x后,Observable.combineLatest导致错误。 - 不能推断类型

时间:2017-07-14 23:11:05

标签: android kotlin rx-java rx-java2

最近几天,我尝试将项目从RxJava 1.x.x迁移到RxJava 2.x.x.我有这个简单的方法。如果我使用rxjava 1.x.x(rx.Observable)中的Observables,一切都会好的。但是,当我用" new" observables(io.reactivex.Observable,我得到一个错误说:"类型推断失败。请明确指定"

var img = new Image();  img.style.top = 500;  var imgX = img.offsetTop;    

你知道我做错了什么吗?你能解释一下RxJava 2中究竟是什么导致了这个错误吗?

有人(here)有一个与此非常相似的问题。但是,他们在他们的解决方案中使用BiFuntion,它接受两个参数并创建结果。如你所见,我使用了更多的参数(3),所以我不能在这里使用Bifunction。

3 个答案:

答案 0 :(得分:1)

您使用的是RxKotlin吗?如果没有,我建议尝试一下,因为它有各种方法的SAM助手,包括combineLatest,这是受影响的方法之一。有关详细信息,请参阅docs

答案 1 :(得分:1)

最后,我根据上面给出的建议找到了解决方案。应该提到的第一件事是我们根本不应该返回可以为空的Observable。如果我们需要检查传递给combineLatest的值,我们应该先执行它,而不是在combineLatest中检查它。

当我们删除可空性时,方法如下:

fun <T1, T2, T3, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, merge: (T1, T2, T3) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, observable3, Function3 {
        value1, value2, value3 ->
        merge(value1, value2, value3)
    })
}

我唯一需要改变的是用Function3替换lambda - 它解决了类型推断的问题。

如果combineLatest只有两个值,则可以使用BiFuntion而不是Function3。

如果你需要将6个变量传递给combineLatest,你可以使用Function6等。

请参阅以下所有变体:

fun <T1, T2, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, merge: (T1, T2) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, BiFunction {
        value1, value2 ->
        merge(value1, value2)
    })
}

fun <T1, T2, T3, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, merge: (T1, T2, T3) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, observable3, Function3 {
        value1, value2, value3 ->
        merge(value1, value2, value3)
    })
}

fun <T1, T2, T3, T4, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, observable4: Observable<T4>, merge: (T1, T2, T3, T4) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, observable3, observable4, Function4 {
        value1, value2, value3, value4 ->
        merge(value1, value2, value3, value4)
    })
}

fun <T1, T2, T3, T4, T5, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, observable4: Observable<T4>, observable5: Observable<T5>, merge: (T1, T2, T3, T4, T5) -> R): Observable<R> {

    return Observable.combineLatest(observable1, observable2, observable3, observable4, observable5, Function5 {
        value1, value2, value3, value4, value5 ->
        merge(value1, value2, value3, value4, value5)
    })
}

fun <T1, T2, T3, T4, T5, T6, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, observable4: Observable<T4>, observable5: Observable<T5>, observable6: Observable<T6>, merge: (T1, T2, T3, T4, T5, T6) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, observable3, observable4, observable5, observable6, Function6 {
        value1, value2, value3, value4, value5, value6 ->
        merge(value1, value2, value3, value4, value5, value6)
    })
}

fun <T1, T2, T3, T4, T5, T6, T7, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, observable4: Observable<T4>, observable5: Observable<T5>, observable6: Observable<T6>, observable7: Observable<T7>, merge: (T1, T2, T3, T4, T5, T6, T7) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, observable3, observable4, observable5, observable6, observable7, Function7 {
        value1, value2, value3, value4, value5, value6, value7 ->
        merge(value1, value2, value3, value4, value5, value6, value7)
    })
}

fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combineLatest(observable1: Observable<T1>, observable2: Observable<T2>, observable3: Observable<T3>, observable4: Observable<T4>, observable5: Observable<T5>, observable6: Observable<T6>, observable7: Observable<T7>, observable8: Observable<T8>, merge: (T1, T2, T3, T4, T5, T6, T7, T8) -> R): Observable<R> {
    return Observable.combineLatest(observable1, observable2, observable3, observable4, observable5, observable6, observable7, observable8, Function8 {
        value1, value2, value3, value4, value5, value6, value7, value8 ->
        merge(value1, value2, value3, value4, value5, value6, value7, value8)
    })
}

答案 2 :(得分:0)

我有同样的问题。然后我看到Kotlin有一个同名的函数,并且默认情况下使用它。所以我只是显式导入了RxJava函数,这帮助我摆脱了错误:

import io.reactivex.functions.Function3