Mark Buikema
11/06/2020, 9:23 PMmapOf()
is not sorted
• sortedMapOf()
is mutable
• hashMapOf()
is mutable and not sorted
• linkedMapOf()
is mutable
Any other suggestions?Nir
11/06/2020, 9:25 PMNir
11/06/2020, 9:26 PMNir
11/06/2020, 9:28 PMRyan
11/06/2020, 9:28 PMNir
11/06/2020, 9:29 PMMap
interfaceNir
11/06/2020, 9:29 PMNir
11/06/2020, 9:29 PMMark Buikema
11/06/2020, 9:29 PMRyan
11/06/2020, 9:29 PMRyan
11/06/2020, 9:30 PMNir
11/06/2020, 9:30 PMMark Buikema
11/06/2020, 9:31 PMmapOf()
, it won't be sorted, right?Nir
11/06/2020, 9:31 PMclass foo<K, V>(val data: SortedMap<K, V>): Map<K, V> by data
more or lessMark Buikema
11/06/2020, 9:33 PMby data
mean?Rob Elliot
11/06/2020, 9:35 PMMark Buikema
11/06/2020, 9:36 PMjbnizet
11/07/2020, 6:57 AMCLOVIS
11/07/2020, 2:05 PMCollections.unmodifiableMap
read-only, not immutable?Nir
11/07/2020, 3:33 PMNir
11/07/2020, 3:34 PMNir
11/07/2020, 3:34 PMNir
11/07/2020, 3:36 PMNir
11/07/2020, 3:37 PMRob Elliot
11/07/2020, 4:38 PMCollections.unmodifiableMap
returns a view of a Map which will throw an exception if you call any mutating method on it, and which does not permit access to the wrapped map. So the returned Map is immutable if the wrapped Map never escapes (unless you do something evil with reflection). Hence the result of Collections.unmodifiableMap(sortedMapOf("x" to 1))
is immutable; this includes the collections returned by keys
and entries
and values
.
Ironically the type of Collections.unmodifiableMap(sortedMapOf("x" to 1))
is MutableMap
! This is because any method returning java.util.Map
is considered as returning a kotlin.collections.MutableMap
.Nir
11/07/2020, 5:10 PMDominaezzz
11/07/2020, 5:54 PMNir
11/07/2020, 6:19 PMCLOVIS
11/07/2020, 6:53 PMCollections.immutable*
returns a wrapper around the original collection, that throws on all attempts to modify itRob Elliot
11/07/2020, 9:05 PMWeird. That allows for some dishonest downcastingIt’s not really -
java.util.Map
dates to before the JVM world was converted to Immutability, so its interface implies mutability. java.util.Collections.unmodifiableMap
consequently had to return a java.util.Map
with all the mutating methods, but make them fail at runtime (though I suppose they could have retrofitted a ReadOnlyMap
supertype…). It’s perfectly reasonable for kotlin to acknowledge java.util.Map
’s apparent mutability by using MutableMap
as a synonym.
Which makes it quite correct that if you call a java method that returns java.util.Map
(like java.util.Collections.unmodifiableMap
) from kotlin you’ll be returned a kotlin.collections.MutableMap
.
The weird bit is that there isn’t a kotlin equivalent to unmodifiableMap
in the stdlib, though perhaps they felt that in practice no-one would downcast the read only interface?Nir
11/07/2020, 10:11 PMNir
11/07/2020, 10:12 PMNir
11/07/2020, 10:14 PMCLOVIS
11/07/2020, 11:05 PMdata class ReadOnlyMap<K, V>(private val map: Map<K, V>) : Map<K, V> by map
Still though, it's read-only, not immutable.CLOVIS
11/07/2020, 11:06 PMconst
functions but I don't think the JVM's capable of having anything like that (maybe with value types from Project Valhalla?)Nir
11/07/2020, 11:52 PMNir
11/07/2020, 11:54 PMCLOVIS
11/08/2020, 9:12 AMsuspend
Rob Elliot
11/08/2020, 10:26 AMAstronaut4449
11/09/2020, 6:58 AMsortedMapOf().toMap()
?Rob Elliot
11/09/2020, 8:25 AMMap.toMap()
- while it returns Map
, it’s actually a MutableMap
so a malevolent/incompetent actor could downcast and mutate it. And it makes a copy of the map it was called on, so you’ll be constructing two map instances. CLOVIS’ ReadOnlyMap
class doesn’t have either issue.CLOVIS
11/09/2020, 9:15 AMMap.toReadOnly()
which is a one-liner and just as easy to use ^^)Astronaut4449
11/09/2020, 9:32 AMRob Elliot
11/09/2020, 9:54 AMReadOnlyMap
instead of ImmutableMap
. But if you don’t allow the original reference to escape then the value it returns isn’t mutable, whereas the value returned by Map.toMap
can always be downcast to MutableMap
and mutated.Rob Elliot
11/09/2020, 9:55 AMRemoved, it was nonsense...
I’m a prat, forgot that SortedMap
is a Java interface so mutable, so all of the returned objects there were mutable 😳Rob Elliot
11/09/2020, 10:07 AMSortedMap
interface, it’s just java.util.SortedMap
, so it’s hard to get the SortedMap
specific methods (like subMap
, firstKey
) without also getting all the mutating methods. I haven’t fully understood how kotlin manages to make kotlin.collections.Map
interchangeable with java.util.Map
without adding the mutating methods to its signature.