Kotlin:具有可空返回值的BiFunction无法编译

问题描述 投票:2回答:1

这个(大大简化的)代码无法为我编译。不知道为什么。返回类型是Entry?null对我来说似乎是一个有效的值。

val foo = BiFunction<Int, List<Entry>, Entry?> { foo:Int, bar:List<Entry> ->
    null
}

错误消息是Null can not be a value of a non-null type Entry

谁能告诉我我错过了什么?

我在用:

ext.kotlin_version = '1.2.10'
compile "io.reactivex.rxjava2:rxjava:2.1.8"
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

我欢迎任何建议。新年快乐!

kotlin rx-java rx-android
1个回答
7
投票

apply类中的BiFunction方法在其返回值(以及其参数)上有一个@NonNull注释。显然你不能通过提供一个可空类型作为类型参数来覆盖它。

您可能不应该:RxJava 2流不能包含null元素(请参阅here)。

© www.soinside.com 2019 - 2024. All rights reserved.