Skip to content

09 · Data Structures

Computed immutable updates

A with update accepts compound operators such as value += 1. It reads the old field once and computes a replacement without mutating the original. Existing visibility, type, conflicting path and linear-copy restrictions still apply.

On this page

A with update accepts compound operators such as value += 1. It reads the old field once and computes a replacement without mutating the original. Existing visibility, type, conflicting path and linear-copy restrictions still apply.

Complete example

Computed immutable updates.

computed_record_updates.zolo
Playground
struct Counter { value: int, limit: int? }

let current = Counter { value: 2, limit: nil }
let next = current with { value += 1, limit ??= 10 }

print(current.value) // 2

print(next.value)    // 3

print(next.limit)    // 10

Rules and behavior

Records, maps and updates · Documentation

From a checkout of the repository:

zolo examples/features/computed_record_updates.zolo

Grouping updates by path

Group changes under their common path instead of repeating it. Compound updates and field checks retain their usual behavior.

08-grouped-updates.zolo
Playground
// Update several fields under the same path, leaving the original value intact.

struct Address { city: str, visits: int }
struct Person { name: str, address: Address }
let original = Person { name: "Ana", address: Address { city: "Recife", visits: 2 } }
let moved = original with {
    address { city: "Natal", visits += 1 },
}
print(original.address.city) // expect: Recife

print(moved.address.city) // expect: Natal

print(moved.address.visits) // expect: 3

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br