5.7 Lambdas and closures
let inc = x => x + 1
let add = (a: i32, b: i32) => a + b
users.map(.name) // = u => u.name
users.filter(.isActive()) // = u => u.isActive()
retryWith(3) { fetch() } // a closure as the last argument
cluster.lock("k", ttl: LOCK_TTL) { migrate() }.fieldand.method(args)in argument position are shorthand for a lambda with one parameter.- Trailing closure: if the last parameter of a function has a function type, the block after the call is passed as that argument:
f(a) { … }or with parametersf(a) { x => … }. - Capture: a non-escaping closure (not stored and not passed to another task) captures by reference; an escaping one — by move.
move x => …is an explicit move. The compiler determines "escaping" from the parameter type:fn(…) escapingor passing tospawn.
See also
Source: docs/spec/oxl-standard.en.md