Skip to content

06 · Functions

Named trailing blocks

Several callback arguments can follow a call as labelled blocks. Labels resolve to callable parameters, so blocks can appear in another order. Creating a callback does not execute its body; mutable captures keep their existing cells.

On this page

Several callback arguments can follow a call as labelled blocks. Labels resolve to callable parameters, so blocks can appear in another order. Creating a callback does not execute its body; mutable captures keep their existing cells.

Complete example

Named trailing blocks.

22-named-callback-blocks.zolo
Playground
// Several callbacks read in the order of the operation they describe.

fn dialog(title: str, content: fn() -> str, confirm: fn() -> bool) -> str {
    let message = content()
    if confirm() { "{title}: {message}" } else { "{title}: cancelled" }
}

let answer = dialog(title: "Delete?") content {
    "This removes one file."
} confirm {
    true
}
print(answer)
// expected: Delete?: This removes one file.


fn transform(value: int, before: fn(int) -> int, finish: fn(int) -> int) -> int {
    finish(before(value))
}
print(transform(3) finish { |value| value * 2 } before { |value| value + 1 })
// expected: 8


// Creating a callback does not run its body. Mutable captures keep their cell.

var calls = 0
fn invoke(work: fn() -> int) -> int = work()
print(invoke() work { calls += 1; calls })
// expected: 1

print(calls)
// expected: 1

Rules and behavior

Function parameters and lambdas · Documentation

From a checkout of the repository:

zolo examples/features/05-functions/22-named-callback-blocks.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br