How Kotlin infers Java method parameter nullability?

Alexey Adamovskiy :

I'm trying to call Java method org.springframework.data.mongodb.core.query.Update#set from Kotlin code. This method is declared as

public Update set(String key, Object value)

There is no additional annotations or comments about parameters nullability. In fact, it just puts value into HashMap, so effectively value might be null.

However, Kotlin compiler treats value type as Any (instead of expected Any!), and fails when passing null with

Null can not be a value of a non-null type Any

Passing nullable value with !! patently causes KotlinNullPointerException, casting as Any throws TypeCastException.

What is the logic behind such nullability inference? Passing null parameter with reflection actually works well, but is there more natural solution?

Alexey Romanov :

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164083&siteId=1