are collections completely immutable by default if...
# getting-started
b
are collections completely immutable by default if you use val?
r
No.
val
just means the variable reference cannot be reassigned. The referenced object itself can be mutated if the object supports mutation.
b
ok, weird. I have a data class which defines a set as val something and I can't do: data.something.remove(stuff)
or kotlin sets just different from java sets in terms of signatures?
r
Is it a
Set
or
MutableSet
?
b
Set
converting from java atm
I suppose I either need MutableSet then or copy
r
Kotlin has mutable and immutable collections.
List
,
Set
are immutable (really not immutable, but read-only, as the underlying collection is the same, just the interface is different).
Yup, on your last comment
b
thanks
a
You don't always need copy -> you can do
mySet.toMutableSet()
and it's a little more readable. 🙂