← the matrixmodel

Capability — model

Closures

A function that remembers where it was born.

Scheme gave lexical scope and first-class functions at once, so a function could capture the variables around it and carry them along. That combination — the closure — is the hinge most modern languages turned on: JavaScript’s counters, Python’s decorators, Rust’s borrow-checked captures, every callback you have ever passed to an event listener.

It replaced a language feature (the “nested procedure with access to enclosing locals”) with a value. You can store one in a struct, send it across a thread, return it from a factory — the capture travels with the code.

The trade-off

  • Closures keep their environment alive, which is how a small callback can pin a large object in memory.
  • Late binding surprises people: a closure over a loop variable sees the final value, unless the language says otherwise.

Where it came from

Scheme, 1975 — lexical scope and closures, distilled from LISP.