When using reflection to investigate collection classes, mutable and immutable classes for the same type are referentially equal.
Why is this the case?
@Test
fun demonstrate_mutableAndImmutableClassesAreTheSame() {
println("(MutableIterable::class === Iterable::class) = ${(MutableIterable::class === Iterable::class)}")
println("(MutableCollection::class === Collection::class) = ${(MutableCollection::class === Collection::class)}")
println("(MutableList::class === List::class) = ${(MutableList::class === List::class)}")
println("(MutableSet::class === Set::class) = ${(MutableSet::class === Set::class)}")
println("(MutableMap::class === Map::class) = ${(MutableMap::class === Map::class)}")
println("(MutableMap.MutableEntry::class === Map.Entry::class) = ${(MutableMap.MutableEntry::class === Map.Entry::class)}")
}
prints
(Iterable::class === MutableIterable::class) = true
(Collection::class === MutableCollection::class) = true
(List::class === MutableList::class) = true
(Set::class === MutableSet::class) = true
kotlin-reflect
is included as a dependency (otherwise all results arefalse
.) This also does not apply to Kotlin/JS (and presumably also native, but I haven't tested that.) – Salem