Skip to content

09 · Data Structures

Nested immutable updates

The with update evaluates its base once and copies the root and changed record prefixes. Unchanged children remain shared. The result preserves its nominal type; conflicting paths and linear resource copies are rejected.

On this page

The with update evaluates its base once and copies the root and changed record prefixes. Unchanged children remain shared. The result preserves its nominal type; conflicting paths and linear resource copies are rejected.

Complete example

Nested immutable updates.

20-immutable-record-updates.zolo
Playground
// Item 4: copy only the record paths being changed, keeping the same type.

struct Address { city: str, postal_code: str }
struct User { name: str, address: Address, roles: [str] }

fn main() {
    let original = User {
        name: "João",
        address: Address { city: "São Paulo", postal_code: "01000-000" },
        roles: ["reader"],
    }
    let changed = original with { name: "Ana", address.city: "Recife" }
    print(original.name)
    // expected: João

    print(changed.name)
    // expected: Ana

    print(original.address.city)
    // expected: São Paulo

    print(changed.address.city)
    // expected: Recife

    print(changed.address.postal_code)
    // expected: 01000-000


    // The untouched roles reference is shared, while address was copied.

    changed.roles.push("reviewer")
    print(original.roles.len())
    // expected: 2

    let next = changed with { address.postal_code: "50000-000" } with { name: "Beatriz" }
    print(next.name)
    // expected: Beatriz

}

Rules and behavior

Records, maps and updates · Documentation

From a checkout of the repository:

zolo examples/features/06-data-structures/20-immutable-record-updates.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br