RxKotlin "withLatestFrom(...)" compile error: not enough information to infer type variable R

RxKotlin "withLatestFrom(...)" compile error: not enough information to infer type variable R

1) 下面的代码编译失败并出现错误:"not enough information to infer type variable R"

keywordChanges
  .withLatestFrom(searchParamsSubject)
  .subscribe { (keyword, searchParams) ->
     ...
  }

2) 下面的代码可以编译并运行,但我不希望有一个空的 subscribe(),也不要将副作用放入组合器函数中。

keywordChanges
  .withLatestFrom(searchParamsSubject) { keyword, searchParams ->
    searchParamsSubject.onNext(searchParams.copy(keyword = keyword))
  }
  .subscribe()

3) 下面是 RxKotlin 库中的代码,我正在尝试调用 1)

/**
 * Emits a `Pair`
 */
inline fun <T, U, R> Observable<T>.withLatestFrom(other: ObservableSource<U>): Observable<Pair<T,U>>
        = withLatestFrom(other, BiFunction{ t, u -> Pair(t,u)  }

我如何修改 1) 中的代码以使其工作?

你必须明确地告诉编译器你正在使用什么类。

        val o1 = Observable.just(1)
        val o2 = Observable.just(2)

        o1.withLatestFrom(o2, BiFunction { t1 : Int, t2 : Int ->  t1 to t2})
            .subscribe { (one, two) -> }

或者,RxKotlin 扩展函数库会为您处理这个问题。 https://github.com/ReactiveX/RxKotlin