12 · Object-Oriented Programming
Keep the remaining record fields
In { name: str, ..R }, R represents the remaining fields of an anonymous record. A generic function can require name while retaining the exact types of the other fields. The row cannot redeclare an explicit field.
On this page
In { name: str, ..R }, R represents the remaining fields of an anonymous record. A generic function can require name while retaining the exact types of the other fields. The row cannot redeclare an explicit field.
Complete example
Keep the remaining record fields.
15-record-rows.zolo
// R carries the exact fields that this function does not mention.
fn rename<R>(user: { name: str, ..R }, name: str) -> { name: str, ..R } =
user with { name: name }
fn twice<R>(user: { name: str, ..R }) -> { name: str, ..R } =
rename(rename(user, "first"), "Ada")
let original = .{ name: "Ana", id: 42, active: true, nested: .{ count: 3 } }
let changed = twice(original)
assert(changed.name == "Ada")
assert(changed.id == 42)
assert(changed.active)
assert(changed.nested.count == 3)
assert(original.name == "Ana")
// A stored generic callable retains the same record contract.
let rename_user = rename
let again = rename_user(changed, "Bia")
assert(again.id == 42)
assert(again.nested.count == 3)
// A closed record parameter can select fields from the preserved result.
fn get_id(user: { id: int }) -> int = user.id
assert(get_id(again) == 42)
print("record rows: ok")
Reference
zolo run examples/features/40-syntax-fourth/15-record-rows.zolo --no-cache
See also