Kotlin: Is Read-Only List immutable?
A
List
is a container, which is an object that holds other objects. Containers are also calledcollections
.
A List
is read-only – you can read its contents but not write to list. However if the underlying implementation is a MutableList
and you retain a mutable reference to that implementation, you can still modify it via that mutable reference.
fun main() {
val a = mutableListOf(1)
val b: List<Int> = a
print(b) // [1]
a += 2
print(b) // [1,2]
}
As we can see, even thought the b
is read only, the value can still be modified since it is referenced to the a which is MutableList.
Therefore, Read-Only List is still modifiable.