07 · Pattern Matching
Keep a tuple's exact tail
Destructure (status, ...payload) to bind the remaining tuple elements together. The payload keeps the type and order of each element. The rest binding must be last and does not turn the tail into a homogeneous array.
On this page
Destructure (status, ...payload) to bind the remaining tuple elements together. The payload keeps the type and order of each element. The rest binding must be last and does not turn the tail into a homogeneous array.
Complete example
Keep a tuple's exact tail.
20-tuple-rest.zolo
fn response() -> (int, str, bool) { return [200, "queued", true] }
fn describe(payload: (str, bool)) -> str = "{payload[0]}: {payload[1]}"
fn summarize(packet: (int, str, bool)) -> str {
let (status, ...payload) = packet
let (...whole) = packet
let (_, _, _, ...empty) = packet
assert(status == 200)
assert(payload[0] == "queued")
assert(payload[1] == true)
assert(whole[0] == 200)
assert(empty.len() == 0)
"{status}: {describe(payload)}"
}
print(summarize(response()))
Reference
zolo run examples/features/40-syntax-fourth/20-tuple-rest.zolo --no-cache
See also