SplitScript reference / Language / closure
closure
syntax
value => expression | (left: T, right: U) -> Result => { ... }
Creates a callable value with lexical captures.
Each parenthesized parameter may be an irrefutable binding pattern and still consumes one argument. Parameter and result types are inferred bidirectionally from the body, invocation sites, and any expected callable type. A single inferred name may omit parentheses; zero, multiple, annotated, or destructured parameters use parentheses. An explicit result uses (parameters) -> Result => body; write async T for an explicitly asynchronous closure or iterator T for a closure that forwards or generates an iterator. The body is any expression, including a value block. An async body may use await or retry; an iterator body becomes a lazy generator only when it uses yield. Calling an async function or generator creates its continuation without executing the 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))