dimanche 12 juillet 2020

Mutable and immutable collection classes are the same kotlin

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





Aucun commentaire:

Enregistrer un commentaire