Lambdas and closures
Clorraine has two lambda forms with different powers:
| Form | Shape | Can be stored? |
|---|---|---|
| Capture-less lambda | (x: i32) => x + 1 | Yes; it is a plain function pointer |
| Capturing closure | [borrow total] (n: i32) => { ... } | No; it is ephemeral and inline-only |
A capture-less lambda matches func(...). A capturing closure used where a plain function pointer is required is E10400.
Capture-less lambdas
let addOne: func(i32): i32 = (x: i32) => x + 1;
let result: i32 = addOne(4);The value is an ordinary function pointer with no environment.
Capturing closures
A capture list makes the environment explicit:
let total: i32 = 10;
([borrow total] (n: i32) => { total = total + n; })(5);- Writing a captured binding requires
[borrow name]. - A duplicable capture is copied; a non-duplicable capture is borrowed read-only.
- A
takecapture is rejected (E10403). - A capturing closure cannot be returned, stored, or captured again (
E10404).
A complete example:
module demo.Lambdas;
import { println } from std.io;
func main(): i32 {
let addOne: func(i32): i32 = (x: i32) => x + 1;
let base: i32 = 10;
let total: i32 = addOne(base);
([borrow total] (n: i32) => { total = total + n; })(5);
println($"total={total}");
return 0;
}Output:
total=16C hooks
A capturing closure may cross a synchronous borrow-mode callback on an ffi declaration. The compiler generates a trampoline and a caller-stack environment. The callback signature must be ABI-identical to the lifted lambda.
The environment must still be live when the C hook calls back. Invoking a capturing callback after its hook returned faults (E11215). A capture-less callback has no environment and no such check.
Why the limits
A capturing closure owns or borrows part of its caller's stack. Storing it would allow it to outlive that stack, so the language rejects the escape instead of leaving a dangling environment. The restrictions are the price of a closure that needs no allocation and no reference counting.