---
title: "Closures"
description: "Closure syntax, parameter inference, block bodies, captures and mutable captures, escaping closures, closure types, and trailing-closure calls."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.atoll.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Closures

A closure is an unnamed function value written with `=>`. It inhabits the same
`fn(A, B) -> R` types as a named function, so anywhere a function value is
accepted, a closure will do.

```atoll
fn main() void {
    double := (v int) => v * 2
    add := (a int, b int) => a + b
    constant := () => 42

    println("${double(21)} ${add(20, 22)} ${constant()}")
}
```

## Parameters

A single parameter may be written bare; zero or several parameters need
parentheses.

```atoll
fn main() void {
    square := (v int) => v * v
    nothing := () => 0
    sum := (left int, right int) => left + right

    println("${square(7)} ${nothing()} ${sum(1, 2)}")
}
```

Parentheses around a multi-parameter list are required — a bare `a, b => ...`
does not parse as a closure:

```atoll fail
fn main() void {
    add := a, b => a + b
    println("${add(1, 2)}")
}
```

Parameter types come from the expected function type when there is one, so a
closure passed as an argument usually needs no annotations at all.

```atoll
fn apply(value int, operation fn(int) -> int) int => operation(value)

fn main() void {
    println("${apply(21, v => v * 2)}")
    println("${apply(21, (v) => v + 1)}")
    println("${apply(21, (v int) => v - 1)}")
}
```

Annotate when nothing supplies the expectation, for example when the closure
is bound to a local before it is ever passed anywhere:

```atoll
fn main() void {
    compare := (left int, right int) => left < right
    format := (label string, value int) => "${label}=${value}"

    println("${compare(1, 2)} ${format("count", 3)}")
}
```

A typed parameter is written as a function's is, the type after the name.
`left: int`, with a `:` between, is the old spelling: it is read the same and
warned (`ATOLL1120`), with the fix `atoll fix` writes. Closure parameters do
not take defaults, and there is no variadic form.

## Bodies

The expression after `=>` is the result. It can be any expression, including
an `if` or a `match`.

```atoll
enum Level { Low, High }

fn main() void {
    classify := (v int) => if v < 0 { "negative" } else { "non-negative" }
    limit := (l Level) => match l {
        Low => 10
        High => 90
    }

    println("${classify(-3)} ${limit(High)}")
}
```

A brace block gives statements and an early `return` out of the closure
itself. The final expression is still the result.

```atoll
fn main() void {
    normalize := (v int) => {
        if v < 0 {
            return -v
        }
        v
    }

    describe := (xs []int) => {
        mut total := 0
        for x in xs {
            total += x
        }
        "${xs.len()} values, total ${total}"
    }

    println("${normalize(-4)} ${describe([1, 2, 3])}")
}
```

`return` inside a closure leaves the *closure*, not the enclosing function.
For the same reason, `error` and `?` belong to the enclosing function's error
contract, not the closure's: a closure cannot raise an error of its own. To
supply a fallible callback, call a fallible function from inside it.

```atoll
error MathError { DivideByZero }

fn checked_div(a int, b int) int ! MathError {
    if b == 0 {
        error DivideByZero
    }
    return a / b
}

fn run(op fn(int) -> Result[int, MathError], v int) int => op(v).unwrap_or(-1)

fn main() void {
    safe := (v int) => checked_div(100, v)
    println("${run(safe, 5)} ${run(safe, 0)}")
}
```

The closure's result type is `Result[int, MathError]`, which is exactly what a
call to `checked_div` produces, so it matches the parameter's function type.

## Captures

A closure may use names from the surrounding scope. The compiler works out the
capture set; there is no capture list in source.

```atoll run=42
fn main() int {
    factor := 3
    scale := (v int) => v * factor
    return scale(14)
}
```

Only referenced outer locals are captured, and a name declared inside the
closure body is local to that invocation rather than a capture. Captures are
established when the closure value is created, so introducing another binding
afterwards does not retarget an existing closure.

A capture may be any type — a `string` and a `List` behave like an `int`:

```atoll run=8
fn main() int {
    name: string = "hello"
    xs: List[int] = [1, 2, 3]
    size := () => name.len() + xs.len()
    return size()
}
```

A closure in the frame that made it borrows what it captures, so the name cannot
be given away while the closure is still called (`ATOLL3102`). One that leaves
takes what it captures: stored in a struct or a list, or handed to `spawn`, it is
that value's owner, and the name is gone. A `.clone()` keeps both.

Capturing does not grant mutability. An immutable binding stays immutable
inside the closure:

```atoll fail
fn main() void {
    count := 0
    bump := () => {
        count += 1
        count
    }
    println("${bump()}")
}
```

### Mutable captures

A `mut` binding can be read and updated through a closure. The closure and the
enclosing scope share one place, so the updates are visible to both.

```atoll run=2
fn make_counter() fn() -> int {
    mut count: int = 0
    return () => {
        count += 1
        return count
    }
}

fn main() int {
    next := make_counter()
    next()
    return next()
}
```

Closures created in the same scope share the same captured place, so several
callbacks can cooperate over one piece of state:

```atoll run=42
fn main() int {
    mut total: int = 0
    add := (v int) => {
        total = total + v
        return total
    }
    read := () => total

    add(42)
    return read()
}
```

The place is the same on every path and at every iteration. A closure built
inside a loop over a `mut` local declared outside it sees each iteration's
value, and the loop sees what the closure wrote:

```atoll run=33
fn apply(f fn(int) -> int, v int) int { return f(v) }

fn main() int {
    mut r := 0
    mut s := 0
    for r < 3 {
        s = s + apply(x => x + r, 10)
        r = r + 1
    }
    return s
}
```

Because that is shared state, invocation order is observable. A captured
mutable local is not an implicit cross-task atomic — concurrent use needs an
API whose ownership contract permits it.

**A closure may not write what the function it is handed holds.** A function
handed a closure and something else may hold that other argument while it
calls the closure — an element of a list it walks, a reference it took into a
block — and read it again after. A closure that writes the same storage would
give back what the function goes on reading, so the call is refused
(`ATOLL1093`). The function's body says which of its arguments it may hold
across a call of which closure, and the closure's body says what it writes; a
closure that only reads, or writes something else, is accepted. Two pointers
to one type may name one block, so a write through either is refused beside
the other.

```atoll refused=ATOLL1093
fn main() int {
    mut xs := [1, 2, 3]
    xs.for_each_item(x => { xs.push(x) })
    return xs.len().to_int()
}
```

**A shared mutable capture may escape, and it costs one block.** The example
above keeps `total` in the frame, because both closures die with it. Return
them — in a struct, say — and the place has to outlive that frame, so the
compiler gives it a block of its own and both environments hold a counted
reference to it. The last environment to go hands it back:

```atoll run=42
struct Tally {
    add  fn(int) -> int
    read fn() -> int
}

fn make_tally() Tally {
    mut total: int = 0
    return Tally {
        add: (v int) => {
            total = total + v
            return total
        },
        read: () => total
    }
}

fn main() int {
    t := make_tally()
    t.add(42)
    return t.read()
}
```

The block is the only case that pays for one. A single escaping closure over a
mutable capture keeps the place in the environment it already has —
`make_counter` above — and closures that stay in their frame keep it in the
frame. Nothing is allocated for either.

## Escaping closures

A closure escapes when it can outlive the activation that created it: when it
is returned, stored in a longer-lived value, or handed to a callback whose
lifetime is unknown. Returning one is ordinary:

```atoll run=42
fn make_adder(amount int) fn(int) -> int => (v int) => v + amount

fn main() int {
    add5 := make_adder(5)
    return add5(37)
}
```

```atoll run=42
fn compose(f fn(int) -> int, g fn(int) -> int) fn(int) -> int => (v int) => g(f(v))

fn main() int {
    add5 := (v int) => v + 5
    double := (v int) => v * 2
    pipeline := compose(add5, double)
    return pipeline(16)
}
```

`make_adder` returns a closure that still needs `amount`, so the compiler
allocates a typed environment for it and keeps the captured values alive as
long as the closure is reachable. A closure that never escapes may instead be
inlined or lowered to a direct call. Neither choice is visible in source: there
is no closure-kind annotation, and no manual environment allocation or release.

## Closure types

A closure's type is a `fn(...) -> ...` type, so named functions and closures
are interchangeable wherever one is expected.

```atoll
fn twice(value int) int => value * 2

fn apply_all(value int, operations []fn(int) -> int) []int {
    mut out: []int = []
    for op in operations {
        out.push(op(value))
    }
    return out
}

fn main() void {
    named: fn(int) -> int = twice
    anonymous: fn(int) -> int = v => v * 2

    results := apply_all(3, [named, anonymous, v => v + 100])
    println("${results.len()} ${results.get(2) ?? 0}")
}
```

The `for` loop is what keeps that example off the run gate; the collection
itself is fine. Indexing one runs:

```atoll run=42
fn double(v int) int => v * 2

fn main() int {
    a := 2
    b := 20
    ops: []fn(int) -> int = [double, (v int) => v + b, (v int) => v * a]

    mut sum := 0
    sum = sum + match ops.get(1) { Some(f) => f(0) None => 0 }
    sum = sum + match ops.get(2) { Some(g) => g(11) None => 0 }
    return sum
}
```

A named function and a lambda sit in one list because they have one type, and
the two capturing elements keep their captures for as long as the list holds
them.

Compatibility covers the parameter types and the result type, including a
`Result` result — a closure is never erased into an untyped callback.

Closure identity is not structural. Two evaluations of the same closure
expression may produce different environments, so do not use a closure as a
registration key; store an explicit identifier alongside it when a callback
must later be found and removed.

```atoll
struct Subscription {
    id       int
    on_event fn(string) -> void
}

fn notify(subs []Subscription, message string) void {
    for s in subs {
        s.on_event("[${s.id}] ${message}")
    }
}

fn main() void {
    subs := [
        Subscription { id: 1, on_event: m => println(m) },
        Subscription { id: 2, on_event: m => println("audit ${m}") }
    ]
    notify(subs, "deploy finished")
}
```

## Trailing-closure calls

When the last argument is a closure, the braces may follow the argument list
instead of sitting inside it. This is how the collection higher-order methods
are normally written.

```atoll
struct Order {
    id    int
    cents int
    paid  bool
}

fn main() void {
    orders := [
        Order { id: 1, cents: 2000, paid: true },
        Order { id: 2, cents: 500, paid: false },
        Order { id: 3, cents: 5000, paid: true }
    ]

    paid := orders.filter { o => o.paid }.collect()
    ids := orders.map { o => o.id }.collect()
    revenue := orders.sum_of { o => o.cents }
    largest := orders.max_by { o => o.cents }
    any_free := orders.any { o => o.cents == 0 }

    println("${paid.len()} ${ids.len()} ${revenue} ${largest?.id ?? 0} ${any_free}")
}
```

Writing the closure inside the parentheses is the same call — the braces are
purely a layout choice, and neither form is preferred by the language:

```atoll
fn main() void {
    values := [1, -2, 3, -4]

    braced := values.filter { v => v > 0 }.collect()
    parenthesised := values.filter(v => v > 0).collect()

    println("${braced.len()} ${parenthesised.len()}")
}
```

Reach for the braces when the callback body spans lines, and keep the
parentheses when it is a short expression sitting among other arguments. The
same form works for user functions:

```atoll
fn each_line(lines []string, body fn(string) -> void) void {
    for line in lines {
        body(line)
    }
}

fn main() void {
    mut shown := 0

    each_line(["alpha", "", "gamma"]) { line =>
        if line.len() == 0 {
            return
        }
        shown += 1
        println("${shown}: ${line}")
    }

    println("shown ${shown}")
}
```

Note that the closure's `return` skips the blank line and continues the loop —
it exits the callback, not `each_line` and not `main`.

Multi-parameter callbacks still need their parentheses inside the braces:

```atoll
fn main() void {
    values := [1, 2, 3, 4]
    total := values.fold(0) { (acc, v) => acc + v }
    println("${total}")
}
```

## A complete example

Closures as struct fields, as parameters, as return values, and over a
captured local — a small rule engine:

```atoll
struct Event {
    name     string
    severity int
    resolved bool
}

struct Rule {
    label  string
    accept fn(Event) -> bool
}

fn all_of(rules &[]Rule) fn(Event) -> bool {
    return e => {
        for r in rules {
            if !r.accept(e) {
                return false
            }
        }
        return true
    }
}

fn count_matching(events &[]Event, accept fn(Event) -> bool) int {
    mut n := 0
    for e in events {
        if accept(e) {
            n += 1
        }
    }
    return n
}

fn main() void {
    events := [
        Event { name: "disk", severity: 3, resolved: false },
        Event { name: "net", severity: 1, resolved: true },
        Event { name: "cpu", severity: 5, resolved: false }
    ]

    threshold := 2
    rules := [
        Rule { label: "open", accept: e => !e.resolved },
        Rule { label: "severe", accept: e => e.severity >= threshold }
    ]

    accept := all_of(rules)
    println("matching: ${count_matching(events, accept)}")

    names := events.filter { e => accept(e) }.map { e => e.name.clone() }.collect()
    println("first: ${names.get(0) ?? "none"}")

    mut scanned := 0
    events.for_each_item { e =>
        scanned += 1
    }
    println("scanned: ${scanned}")
}
```

`all_of` returns an escaping closure that captures the `rules` list; each
`Rule.accept` captures either nothing or the local `threshold`; and the
`for_each_item` callback mutates the captured `scanned`.

## Review

Before a closure crosses the statement that created it, check:

1. which outer names it captures, and which of those are `mut`;
2. whether the consumer may retain it or invoke it concurrently;
3. what the environment keeps alive — capture a small stable identifier
   instead of a large object graph when the callback can reload what it needs;
4. whether it can suspend while a captured reference is still live.

A closure passed to a suspend-capable function is treated as escaping unless
analysis proves otherwise, which is why an immediately invoked closure can
optimize differently from one stored in a task or a stream callback.

## Captures and memory

The blocks marked `run=` above are compiled and executed by the compiler's own
test suite, and their heaps are checked back to zero.

An environment is an ordinary block: it is reference-counted like any other,
and it dies with the last closure holding it. **A struct field holding a
closure, inside a collection, is ordinary too**: closures get a static filter
of their own, and an environment that can reach itself is allocated on the
traced ladder with a shadow count like any other block, so the cycle scan walks
it.

```atoll run=1
struct Rule {
    accept fn(int) -> bool
}

fn at_least(threshold int) Rule {
    return Rule { accept: (n int) => n >= threshold }
}

fn main() int {
    mut rules: []Rule = []
    rules.push(at_least(3))
    rules.push(at_least(5))
    mut hits: int = 0
    for r in rules {
        if (r.accept)(4) { hits = hits + 1 }
    }
    return hits
}
```

What is worth knowing when you write one is that **a capture is an edge whether
or not the captured name was `mut`**: a `^T` is an address, and the fields
behind it are writable by anyone holding it, so the write that closes a cycle
is usually not the capturing frame's. See
[Cycles](https://docs.atoll.workreef.dev/language/memory/cycles/#closures-build-cycles-too).

Source: https://docs.atoll.workreef.dev/language/fundamentals/closures/index.mdx
