Getting Started
Add Mutator to your project, annotate your data classes, and start mutating.
1. Add the dependencies
Mutator uses KSP (Kotlin Symbol Processing) to generate code at compile time. You need two dependencies: the runtime library and the KSP processor.
In your build.gradle.kts:
plugins {
id("com.google.devtools.ksp") version "<ksp-version>"
}
dependencies {
// Runtime library
implementation("io.peekandpoke.mutator:core:0.117.0")
// KSP processor — generates .mutator() and .mutate {} extensions
ksp("io.peekandpoke.mutator:ksp:0.117.0")
} Find the latest version on Maven Central.
2. Annotate your data classes
Add @Mutable to any data class you want to mutate:
import io.peekandpoke.mutator.Mutable
@Mutable
data class Address(
val street: String,
val city: String,
val zip: String,
) Build your project. KSP generates extension functions for every @Mutable class:
Address.mutator()— creates a long-livedMutator<Address>Address.mutate { ... }— one-shot mutation, returns a newAddress- Typed property setters:
mutator.street = "...",mutator.city = "...", etc. - Collection extensions:
List<Address>.mutator(),Set<Address>.mutator(),Map<K, Address>.mutator()
3. Your first mutation
The simplest way to use Mutator is the mutate { } function. It creates a mutator, applies your
changes, and returns a new immutable instance:
val address = Address(street = "123 Main St", city = "Springfield", zip = "62701")
val updated = address.mutate {
street = "456 Oak Ave"
city = "Shelbyville"
}
// updated == Address("456 Oak Ave", "Shelbyville", "62701")
// address is unchanged — still Address("123 Main St", "Springfield", "62701") 4. Nested mutations
When data classes reference other @Mutable data classes, their mutators compose automatically:
@Mutable
data class Person(
val firstName: String,
val lastName: String,
val age: Int,
val address: Address,
)
val person = Person(
firstName = "John",
lastName = "Doe",
age = 42,
address = Address(street = "Main St", city = "Springfield", zip = "62701")
)
// Nested mutation — no copy() chains
val updated = person.mutate {
firstName = "Jane"
address {
street = "456 Oak Ave"
city = "Shelbyville"
}
}
// updated == Person("Jane", "Doe", 42, Address("456 Oak Ave", "Shelbyville", "62701")) The address { } block opens a sub-mutator. Any changes inside it automatically propagate back up
to the parent Person.
5. Long-lived mutators
For form state management, you typically keep a mutator alive across multiple interactions:
val mutator = person.mutator()
// User edits the name field
mutator.firstName = "Jane"
// User edits the address
mutator.address.street = "456 Oak Ave"
// Check if anything changed
mutator.isModified() // true
// Read the current state
val current = mutator()
// current == Person("Jane", "Doe", 42, Address("456 Oak Ave", "Springfield", "62701"))
// Save: accept changes as the new baseline
mutator.commit()
mutator.isModified() // false What KSP generates
For a data class like Address, KSP generates code equivalent to:
// Generated: mutator creator
fun Address.mutator() = ObjectMutator(this)
// Generated: one-shot mutation
fun Address.mutate(mutation: ObjectMutator<Address>.() -> Unit): Address =
mutator().apply(mutation).get()
// Generated: typed property accessors on Mutator<Address>
var Mutator<Address>.street
get() = get().street
set(v) = modifyIfChanged(get().street, v) { it.copy(street = v) }
var Mutator<Address>.city
get() = get().city
set(v) = modifyIfChanged(get().city, v) { it.copy(city = v) }
var Mutator<Address>.zip
get() = get().zip
set(v) = modifyIfChanged(get().zip, v) { it.copy(zip = v) }
// Generated: collection mutators
fun List<Address>.mutator() = mutator(child = { mutator() })
fun Set<Address>.mutator() = mutator(child = { mutator() })
fun <K> Map<K, Address>.mutator() = mutator(child = { mutator() }) For nested @Mutable properties (like Person.address), KSP generates a sub-mutator with
onChange propagation:
// Generated: sub-mutator with automatic back-propagation
val Mutator<Person>.address
get() = get().address.mutator()
.onChange { address -> modifyValue { get().copy(address = address) } } This is the magic: when you set mutator.address.street = "...", the address mutator notifies the
person mutator via onChange, which rebuilds the person with the new address. All of this happens
automatically.
Controlling discovery with deep
By default, @Mutable recursively discovers all referenced types and generates mutators for them. You
can control this with the deep parameter:
// Default: deep = true — discovers and generates mutators for all referenced types
@Mutable
data class Person(val address: Address)
// Address gets a mutator too, even without its own @Mutable annotation
// Shallow: deep = false — only this class gets a mutator
@Mutable(deep = false)
data class Order(
val id: String,
val meta: ExternalConfig, // simple get/set (ExternalConfig has no mutator)
val address: Address, // .mutator() accessor (Address IS @Mutable)
) With deep = false, referenced types only get .mutator() accessors if they are
independently marked @Mutable (or pulled in by another deep = true class). Otherwise,
they get simple get/set accessors — like a String or Int.
This is useful when you want to make a class with external types mutable without pulling in the entire type graph. Conversely, if you need mutators for external types not in your codebase, use the trigger pattern:
// Trigger mutator generation for types you don't own
@Mutable(deep = true)
data class TriggerMutators(
val ext: SomeExternalLibraryType,
)
// SomeExternalLibraryType now gets a mutator generated Excluding types with @NotMutable
Use @NotMutable to explicitly prevent mutator generation for a type, even if it would be discovered
transitively:
@NotMutable
data class Secret(val key: String, val value: String)
@Mutable
data class Config(
val name: String,
val secret: Secret, // simple get/set — Secret is @NotMutable
) @NotMutable takes priority over transitive discovery. The property gets a simple get/set accessor
instead of a .mutator() accessor.