Skip to content

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
Playground
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

Reference · Source program

zolo run examples/features/40-syntax-fourth/20-tuple-rest.zolo --no-cache

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br