Add explanation that read-only variable (val) can refer to mutable object (e.g. List)
Opened this issue · 1 comments
In the section Introduction under subsection Variables it would be very helpful if you showed an example where read-only variable refers to mutable object such as list with an explanation that variable itself is immutable, but object it refers to is mutable.
Because later in the subsection Generics there is this code:
class MutableStack<E>(vararg items: E) {
private val elements = items.toMutableList()
fun push(element: E) = elements.add(element)
...
}
This part was very unclear to me, it didn't quite get how can you add items to variable elements
if the variable is immutable. Until I read at this source the following:
Also, while the read-only variable may not be reassigned while it is in scope, it can still refer to an object which is in itself mutable (such as a list).
That's why I think it would be very useful if you added explanation about immutable variables referring to mutable objects in the section Variables.
@SebastianAigner Can I take this one?