1)下面的代码编译错误:“没有足够的信息来推断变量R”
keywordChanges
.withLatestFrom(searchParamsSubject)
.subscribe { (keyword, searchParams) ->
...
}
2)下面的代码编译并工作,但我不希望有一个空的subscribe()
,也不希望在组合器函数中添加副作用。
keywordChanges
.withLatestFrom(searchParamsSubject) { keyword, searchParams ->
searchParamsSubject.onNext(searchParams.copy(keyword = keyword))
}
.subscribe()
3)下面是来自RxKotlin库的代码,我试图调用它
/**
* 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)中修改代码使其工作?
发布于 2019-09-13 10:55:30
您必须具体地告诉编译器您使用的是哪些类。
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
https://stackoverflow.com/questions/57063614
复制