无法在Observable中调用特定的超时重载; kotlin无法匹配参数

问题描述 投票:0回答:1

我不确定这是Kotlin问题还是RxJava问题,但我倾向于前者。

我正在尝试在Observable上调用特定的timeout重载,而kotlin编译器不允许我这样做。这是示例代码:

fun <T, U> Observable<T>.emitOnTimeout(item:T, itemPredicate:(T)->ObservableSource<U>):Observable<T> {
    return this.publish {
        it.timeout( itemPredicate,
                Observable.just(item).concatWith(Observable.error(Exception()))
        ).retry()
    }
}

编译器在超时调用中抱怨以下内容:

 None of the following functions can be called with the arguments supplied: 
@CheckReturnValue @SchedulerSupport public final fun <V : Any!> timeout(p0: ((T) -> (ObservableSource<(U#1 (type parameter of com.smartlinxsolutions.clockprototype.views.badgeentry.emitOnTimeout)..U#1?)>..ObservableSource<U#1!>?)..((T) -> ObservableSource<U#1!>!)?), p1: ((Observer<in T!>) -> Unit)!): Observable<T!>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun <U : Any!, V : Any!> timeout(p0: ((Observer<in (???..???)>) -> Unit)!, p1: ((T) -> ObservableSource<(???..???)>!)!): Observable<T!>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun <U : Any!, V : Any!> timeout(p0: ObservableSource<(???..???)>!, p1: Function<in T!, out ObservableSource<(???..???)>!>!): Observable<T!>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun <V : Any!> timeout(p0: Function<in T!, out ObservableSource<(???..???)>!>!, p1: ObservableSource<out T!>!): Observable<T!>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun timeout(p0: Long, p1: TimeUnit!): Observable<T!>! defined in io.reactivex.Observable

我什至不知道Kotlin在哪里得到了需要观察者lambda的重载。同样,即使参数应该完全匹配,它似乎也无法匹配该调用。

android kotlin rx-java2
1个回答
0
投票

由于this article直到我发布了这个问题才出现,所以我能够确定如何解决歧义,这与Kotlin时不时遇到的SAM类型冲突问题有关。] >

无论如何,将代码更改为此:

fun <T, U> Observable<T>.emitOnTimeout(item:T, itemPredicate:(T)->ObservableSource<U>):Observable<T> {
    return this.publish {
        it.timeout( io.reactivex.functions.Function(itemPredicate),
                Observable.just(item).concatWith(Observable.error(Exception()))
        ).retry()
    }
}

并且代码将按预期编译。

© www.soinside.com 2019 - 2024. All rights reserved.