Expand

defer

delays execution until function return

<defer> = defer <command>

defer's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

cleanup after processing is done
fn main {
    # create a temp file
    $tmp := mktemp
    defer rm -f $tmp

    # do processing
}
deferred functions calls are executed in last-in-first-out order
fn main {
    for $i in (range 10) {
        defer print ($i ** 2)
    }
}

See Also