Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: BiFunction with nullable return value fails to compile

This (greatly simplified) code fails to compile for me. Not sure why. The return type is Entry? and null seems like a valid value to me.

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

The error message is Null can not be a value of a non-null type Entry

Can anyone tell me what I'm missing?

I am using:

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

I welcome any suggestions. Happy New Year!

like image 251
George Madrid Avatar asked Jan 04 '23 00:01

George Madrid


1 Answers

The apply method in the BiFunction class has a @NonNull annotation on its return value (as well as on its parameters). Apparently you can't override this by providing a nullable type as the type argument.

You probably shouldn't either: RxJava 2 streams can't have null elements in them (see here).

like image 103
zsmb13 Avatar answered Jan 05 '23 17:01

zsmb13