Chapter II · Idioms & deeper language / Lesson 9 of 27

Closures & upvalues

You'll learn: Capture an outer-scope binding and watch the closure op build the function at runtime.

Inner functions capture outer locals *by reference* — these captured bindings are called *upvalues*. Two closures over the same local share that storage. Run this snippet and toggle Bytecode to see the closure opcode and the upvalue descriptors on the inner prototype.

closures.lua
Output idle
Hit Run to execute.
Try it: Make make_adder return *two* closures, one that adds and one that subtracts. They should share the same n.