Considering Kotlin Java Interop: Null Safety and Platform Types
Why is code like this legal in Kotlin?
fun envString(key: EnvVars): String {
return System.getenv(key.toString())
}
getenv()
can return null, as indicated by its JavaDoc:
Returns: the string value of the variable, or null if the variable is not defined in the system environment
So, learning Kotlin partly because it enforces strict(er) null-checks I am baffled by this design decision. It seems very counter the premise of "no more NPE" that supposedly underlies Kotlin.
Is there any document providing the reasoning behind this design?
Side note; As a user of Kotlin, would it be good practice to simply treat all non-primitive types coming from Java interop as nullable explicitly in my Kotlin code?