SplitScript reference / Language / closure

closure

syntax

value => expression | (left: T, right: U) -> Result => { ... }

Creates a callable value with lexical captures.

Parameter and result types are inferred bidirectionally from the body, invocation sites, and any expected callable type. A single inferred parameter may omit parentheses; zero or multiple parameters use parentheses. An explicit result uses (parameters) -> Result => body; write async T as the result when the closure itself is explicitly asynchronous. The body is any expression, including a value block, and may use await or retry to infer an async result. Calling such a closure creates a typed future; creating the closure itself does not execute or poll its body. Captured immutable values are retained in the closure environment. A mutable local is captured by reference through one shared cell, so assignments in the closure and its declaring scope observe each other even after the closure is returned or stored across await. return exits the closure itself; break and continue cannot escape into an outer loop.

Examples

Write an explicit result type

let widen = (value: u16) -> u32 => value as u32

Pass behavior to a function

let doubled = apply(4, value => value * 2)

Capture and update a local

let counter = 0u32
let increment = () => {
    counter += 1
    return counter
}

Suspend inside a closure

let afterTick = (value: u32) => {
    await nextTick()
    return value + 1
}
print(await afterTick(4))