2 ms·
That's wrong, a lot of JDK api doesn't return null (and a lot of API throws NPE for null arguments). Assuming that everything can return or take null would make
by vbezhenar 9y ago
That's wrong, a lot of JDK api doesn't return null (and a lot of API throws NPE for null arguments). Assuming that everything can return or take null would make Kotlin code unnecessary verbose. It's a valid approach, but not the most pragmatic one.
- imtringued 9y agoI assume it inserts an automatic nullcheck [1] if you assign the return value of a java method into a non-null kotlin variable. You might still see NPEs but they happen exactly at the interop boundary between java and kotlin. When you do get an NPE the fix is obvious and trivial to implement. With scala the NPE could appear after the null crossed the java/scala boundary deep into scala code where you'd assume it should be impossible to get an NPE. It can sometimes be hard to fix because the problem is far away from the cause. [1] https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types https://kotlinlang.org/docs/reference/java-interop.html#null... >If we choose a non-null type, the compiler will emit an assertion upon assignment. This prevents Kotlin's non-null variables from holding nulls. Assertions are also emitted when we pass platform values to Kotlin functions expecting non-null values etc. Overall, the compiler does its best to prevent nulls from propagating far through the program (although sometimes this is impossible to eliminate entirely, because of generics).