【问题标题】:Generic Function References in Rx and Kotlin -- type inference failedRx 和 Kotlin 中的泛型函数引用——类型推断失败
【发布时间】:2018-01-26 19:33:12
【问题描述】:

我正在用 Kotlin 写一个方法:

fun fetchDepositSession(): Completable =
        Observable.fromIterable(session.accounts)
                .map(DepositSession::DepositAccount)
                .toList()
                .doOnSuccess(depositSession::depositAccounts::set)
                .flatMapObservable(Observable::fromIterable)
                .map(DepositSession.DepositAccount::account::get)
                .toCompletable()

.flatMapObservable(Observable::fromIterable) 行导致错误:

【问题讨论】:

  • Observable.fromIterable 是一个通用的静态函数。以您当前使用它的方式,您没有声明类型参数,因此无法解析泛型类型。当您将值作为参数传递时,存在,但在您提供的用例中没有,因为您没有提供参数。

标签: android kotlin rx-java rx-java2 rx-kotlin


【解决方案1】:

RxJava 和 Kotlin 推断类型不能很好地工作。有几个问题,例如 KT-13609 和 KT-14984。

请参阅与问题相关的this question。还有一个issue in RxKotlin's Github在谈论它。

无论如何,你总是可以使用:

.flatMapObservable { Observable.fromIterable(it) }

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 2018-08-21
    • 2020-12-30
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多