y
01/23/2023, 1:33 PMSortedMap
equivalent that is a pure Kotlin type (and so doesn’t use platform types)?Sam
01/23/2023, 1:35 PMy
01/23/2023, 1:41 PMRob Elliot
01/23/2023, 1:46 PMlistOf
and mapOf
both return Java lists and mapsSam
01/23/2023, 1:46 PMSortedMap
, all of the methods it inherits from the Map
and MutableMap
interfaces will have proper Kotlin nullability information.y
01/23/2023, 1:48 PMsomeMap[idx] = foo
where someMap
is SortedMap<K,V>
and foo
is V?
, but was not warnedSam
01/23/2023, 1:51 PMSortedMap<K, V>
it lets me try to add null values, but if I declare it as MutableMap<K, V>
(the Kotlin type) it doesn’t.Sam
01/23/2023, 1:51 PMy
01/23/2023, 1:51 PMy
01/23/2023, 1:52 PMRob Elliot
01/23/2023, 1:54 PMval map: TreeMap<String, String> = TreeMap()
val x: String? = "s"
map[x] = "s"
Compiles:
val map: SortedMap<String, String> = TreeMap()
val x: String? = "s"
map[x] = "s"
Rob Elliot
01/23/2023, 1:55 PMTreeMap
can inherit Kotlin's nullability semantics from Map
I'd have thought SortedMap
could, but evidently not.y
01/23/2023, 1:57 PMTreeMap
instead of SortedMap
?Rob Elliot
01/23/2023, 1:58 PMy
01/23/2023, 1:58 PMdmitriy.novozhilov
01/23/2023, 4:10 PMIfcan inherit Kotlin's nullability semantics from Map I'd have thoughtTreeMap
could, but evidently not.SortedMap
TreeMap
is a specific implementation, and Kotlin knows that it treats nullability carefully. But SortedMap
is a generic interface, and there are no guarantees, that someone would provide incorrect implementation of it. So that's why TreeMap
works well and SortedMap
doesn'tephemient
01/23/2023, 4:50 PMSam
01/25/2023, 10:17 AMThis explanation does make sense, except thatis a specific implementation, and Kotlin knows that it treats nullability carefully. ButTreeMap
is a generic interface, and there are no guarantees, that someone would provide incorrect implementation of it.SortedMap
TreeMap
isn’t a final class. In the same way that someone could provide an “incorrect” implementation of SortedMap
, couldn’t they do the same thing by extending TreeMap
?dmitriy.novozhilov
01/25/2023, 10:19 AMjava.util.TreeMap
and change its behavior on null keys and values