Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

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

@dosvarog

Description

@dosvarog

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions