Version: 1.1.0
Closures
A closure is a function that captures names from the scope where it is created.
Essentials
- Capture by copy when you need a snapshot.
- A mutable capture requires the captured name to outlive every call.
- Closures can be passed to
Listmethods and other higher-order functions. - A closure that raises forces the caller to handle that error.
- Prefer a small named function when the capture list gets hard to read.
Example
def main():
var base = 10
fn add(n: Int) -> Int:
return n + base
print(add(5))
Keep in mind
Capturing a reference to a local and returning the closure is rejected when the local would die first.