Version: 1.1.0
Closure declarations
A closure declaration is a nested function plus the names it captures.
Essentials
- The capture is the set of outer names the body uses.
- Explicit capture lists document copies and moves.
- A closure has the same parameter and argument syntax as a function.
- Returning a closure extends the lifetime of what it captured, or the program does not compile.
Example
def adder(base: Int) -> fn(Int) -> Int:
fn inner(n: Int) -> Int:
return n + base
return inner
Keep in mind
If the capture list is longer than the body, the closure should be a struct with a method.