【问题标题】:RxKotlin "withLatestFrom(...)" compile error: not enough information to infer type variable RRxKotlin "withLatestFrom(...)" 编译错误:没有足够的信息来推断类型变量 R
【发布时间】:2019-07-16 18:41:56
【问题描述】:

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) 中的代码以使其正常工作?

【问题讨论】:

    标签: kotlin rx-kotlin


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      相关资源
      最近更新 更多