Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explanation that read-only variable (val) can refer to mutable object (e.g. List) #147

Open
dosvarog opened this issue Apr 4, 2021 · 1 comment

Comments

@dosvarog
Copy link

dosvarog commented Apr 4, 2021

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.

@deepxcode
Copy link
Contributor

@SebastianAigner Can I take this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@dosvarog @deepxcode and others