Hey! This seems inconsistent. Does anyone know why...
# announcements
m
Hey! This seems inconsistent. Does anyone know why? @thana
Copy code
import java.util.*

fun compiles() {
    val map: AbstractMap<String, String> = TreeMap()
    map["key"] = null // non-null not enforced (platform type)
}

fun `does not compile` () {
    val map = TreeMap<String, String>()
    map["key"] = null // non-null enforced? Why? Isn't this also a platform type?
}
3
More specifically:
Copy code
import java.util.*

fun compiles() {
    val map = TreeMap<String, String>() as AbstractMap<String, String>
    map["key"] = null // non-null not enforced (platform type)
}

fun `doesn't compile` () {
    val map = TreeMap<String, String>()
    map["key"] = null // non-null enforced in TreeMap, but not AbstractMap?
}
n
does the IntelliJ popup show anything significantly different for
put
for the two calls?
m
Which popup do you mean? This one? It’s identical for the two
set
calls.
n
yeah...that's the one. hmm.
m
Nice trick that Jetbrains used here: In order to add the assignment syntax for these platform types they just defined a set method as an operator extension function. Sweet. It still doesn’t work when I use put explicitly, and there the only difference in the popups is TreeMap vs. AbstractMap.
Incidentally, I always found it odd that
Map
uses
put
in Java, but
Set
uses
set
.
r
Okay this is some black magic
n
I'm seeing something different from you it looks like
correction, they're both extension method calls
I think Kotlin must have some tricks for identifying what is actually a MutableMap, and AbstractMap isn't included
well, that's not quite right either...in any case agreed, magic
a
@Michael Böiers Good catch. Can you please report this to http://kotl.in/issue. I'm not sure if this is a bug, but it would be nice to keep track of the problem.
👍 1
m
t
@Michael Böiers thank you very much for investigating on that issue <3
🙌 1