Skip to workspace
Kotlin Lab

Working with data / Chapter 03

Revise a release ticket

Copy a value with one changed field and keep the original ticket usable.

The decision

A release dashboard keeps both the submitted ticket and a tagged revision. Append one label to the revision, preserving the owner and the original ticket’s labels.

Read the Kotlin

Track the ticket, its list, and each label

A data class generates copy for its primary-constructor properties. A new ticket gets the same list reference unless you pass a replacement. Copying the list separates additions and removals, but both lists still refer to the same element objects. Ask which of those three layers a later edit changes.

val names = mutableListOf("ops")
val old = Ticket("Ira", names)
val next = old.copy()
names.add("beta")  // visible via next.labels as well

Use the current value of an omitted copy argument

A fresh constructor call applies the class’s declared defaults to omitted properties. A call to old.copy(...) takes its omitted arguments from old’s current constructor properties. This matters when a user has changed a value away from its default, or a new field is added after the tagging function was written.

data class Alert(val text: String, val rank: Int = 1)
val urgent = Alert("down", 8)
urgent.copy(text = "restored")  // rank stays 8

Find the boundary of the generated copy

Only the properties declared in the primary constructor participate in the generated copy arguments. A property initialized in the class body runs its initializer on the new instance. Move state into the constructor or copy it explicitly if the new instance needs to inherit its current value.

data class Session(val owner: String) { var reads = 0 }
val s = Session("Jo")
s.reads = 5
s.copy().reads  // 0

The full example

Java 25
import java.util.ArrayList;
import java.util.List;

class Lesson {
    record Ticket(String owner, List<String> labels) {}

    static Ticket tag(Ticket ticket, String label) {
        var next = new ArrayList<>(ticket.labels());
        next.add(label);
        return new Ticket(ticket.owner(), List.copyOf(next));
    }
}
Kotlin/JVM
data class Ticket(val owner: String, val labels: List<String>)

fun tag(ticket: Ticket, label: String): Ticket =
    ticket.copy(labels = ticket.labels + label)

Inputs and outcomes

InputOutcomeWhy it matters
Mina · [build], add betaMina · [build, beta]The original still has [build].
Omar · [], add rcOmar · [rc]An empty list accepts its first label.
Mina · [beta], add betaMina · [beta, beta]The log records both applications.

Walk through the code

  1. Java constructs a new record and spells out each component to retain. Kotlin’s generated copy function defaults each primary-constructor property to the current value, so owner stays Mina.
  2. copy is shallow. Passing no labels argument would share the existing list. The + operation makes a list containing the old elements followed by the new label; it leaves the old list alone.
  3. This ticket contains strings. If its labels were mutable Label objects, changes inside a Label could still be visible through both tickets.

The Java habit

A val property prevents reassignment. It cannot protect a mutable list passed to the constructor from changes through another reference.

When to choose differently

Appending this way copies the list on every revision. If a batch applies thousands of labels, collect them in a mutable builder and create the final ticket once.

References

Open quizTry code