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

Cannot call specific timeout overload in Observable; kotlin cannot match the arguments

我不完全确定这是 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 的重载。同样,它似乎无法匹配调用,即使参数应该完全匹配。

多亏了 直到我发布这个问题才出现,我能够确定如何解决歧义,这与 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()
    }
}

并且代码将按预期编译。