menu
333 tokens
Captures
Functions, lambdas and captures.
Captures
- capture
- the default is "managed" / automatic as in TS, which means we don't have to think about captures, but incur some allocation cost
1function offset(amount: int32): (value: int32) => int32 {2 return (value) => value + amount;3}Capture Modes
- on demand when desired we can specify
@capturefor lambdas / nested functions @capturewith"move","borrow","copy","manage", ..@capture("manage")preserves identity,@capture("borrow")borrows the original binding,@capture("copy")snapshots it, and@capture("move")transfers it
1@capture({2 default: "copy",3 socket: "move",4 logger: "borrow",5 this: "borrow",6})7return (message) => {8 logger.info("sending");9 return socket.write(`${this.prefix}: ${message}`);10};"repeatable"is the default multiplicity, while"once"is affine and consumed by its first invocationclosures preserve lexical
thisand useFunction<Parameters, Return, Multiplicity>;lambdas (fat pointers with env)
Function,^Function, and&Functionuse managed, owned, and borrowed environmentsrepeatable calls require
&Functionor stronger access and preserve the environment&readonly Functioncannot be called;&exclusive Functiongrants the required mutable accessonly
^Function<Parameters, Return, "once">is valid; its call consumes the callable