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 @capture for lambdas / nested functions
  • @capture with "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 invocation

  • closures preserve lexical this and use Function<Parameters, Return, Multiplicity>;

  • lambdas (fat pointers with env)

  • Function, ^Function, and &Function use managed, owned, and borrowed environments

  • repeatable calls require &Function or stronger access and preserve the environment

  • &readonly Function cannot be called; &exclusive Function grants the required mutable access

  • only ^Function<Parameters, Return, "once"> is valid; its call consumes the callable