There is a bit of inconsistency from my level of understanding. Let’s say we have code interop with Java, assuming MyJavaClass.getNullString is a static method that returns null:
val s1: String = null // Does not compile, s1 is not nullable
val s2: String = MyJavaClass.getNullString() // Compiles, throws IllegalStateException at runtime
I think it doesn’t make sense to let the second line here to compile. It will be more reasonable to force nullability on a value returned from Java.
Then handling it like this if you don’t want it be nullable: val s2: String = MyJavaClass.getNullString() ?: ""
Thoughts?
It would make Java interop very painful. But current situation isn’t great either. I think, Java libraries should be annotated with (possible external) nullability annotations and a tool should exist which will generate those annotations using a bytecode analysis. There would be issues with versions, etc, but it would be an improvement over current situation IMO anyway.