← All examples

Whichever

Whichever, timers, structured tasks, a task that fails.

whichever.zg
# whichever, timers, structured tasks, a task that fails (design/concurrency.md, 2026-09-22).
fn fetch [lent 'url': str.utf8] -> str.utf8 or fail[no_network] {
    if 'url' equal_to "down.com" { fail no_network; }
    return "page of " ++ 'url';
}

MAIN {
    let 'inbox' := channel[str.utf8][max 10];
    let 'shutdown' := channel[bool];
    concur with [lend 'inbox'] { channel.send[lend 'inbox', value "first"]; channel.send[lend 'inbox', value "second"]; }
    concur with [lend 'shutdown'] { channel.send[lend 'shutdown', value true]; }
    let 'deadline' := std::time.after[5 seconds];
    for 'i' from 1 to 4 by 1 {
        whichever {
            'msg' from lend 'inbox'    { std::print.useful["got ", 'msg', newln]; }
            'why' from lend 'shutdown' { std::print.useful["shutdown", newln]; }
            'now' from lend 'deadline' { std::print.useful["timed out after ", 'now', newln]; }
        } otherwise { closed { std::print.useful["all closed", newln]; } };
    }
    {
        concur { std::print.useful["task in a block", newln]; }
        std::print.useful["block body", newln];
    }
    std::print.useful["after the block", newln];
    concur with [lend 'inbox'] { let 'p' := fetch["down.com"] otherwise fail; channel.send[lend 'inbox', value give 'p']; }
        otherwise { no_network { std::print.useful["offline", newln]; } };
    let 'tick' := std::time.every[1 second];
    let 't' := channel.take[lend 'tick'] otherwise 0 seconds;
    std::print.useful["tick ", 't', newln];
}
Output
got first
got second
shutdown
timed out after 5 seconds
block body
task in a block
after the block
offline
tick 1 second