olem
02/19/2018, 7:42 PMMutableMap<K, V>.put(key: K, value: V): V?
, value
requires String
and receives null
.
- Expression 2 compiles and throws NPE. I suppose it's because the Java function put
in ConcurrentMap
is called, so we loose null safety.
- Expression 3 does not compile. Why? Is it because the compiler knows the contract of ConcurrentHashMap
and knows that values cannot be null
? And how does it know this contract?
- Expression 4 does compile and throws NPE. I can't explain that because it's still a ConcurrentHashMap
, this time with a Kotlin extension function.
- Expression 5 compiles and throws NPE. So after all, the Kotlin compiler does not know that ConcurrentHashMap
cannot be null
. So why did expression 3 not compile?
In any case, I know that I cannot put a null value in ConcurrentHashMap
, but I'd like to understand why the Kotlin compiler cannot warn me (and why it does warn me under certain conditions).