#separator:tab
#html:true
#notetype:Basic
#tags column:3
#deck:cracked.guide::Crafting Interpreters::Ch 25 Closures
#columns:Front	Back	Tags
Crafting Interpreters ch 25, Closures: what does it cover?	Closures in clox with upvalues: variables captured from enclosing functions live on the stack until their frame returns, then get &quot;closed&quot; by moving them to the heap.	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::about
Crafting Interpreters ch 25, Closures: why does it matter?	Upvalues are Lua's clever design for fast closures. This is widely considered the hardest chapter in the book and the most satisfying to get right.	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::why
Crafting Interpreters ch 25, Closures: which part of the book is it in?	clox (bytecode VM, C)	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::part
Upvalue: what is it?	An upvalue is a reference from a closure to a local variable of an enclosing function, which stays valid after that function returns.	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::definition
Upvalue: Does jlox use upvalues?	No. jlox closures hold a reference to their whole enclosing environment, a heap-allocated Java map, so the problem never arises.	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::definition
Crafting Interpreters ch 25: what traps should you watch for?	<ul><li>Keeping the open upvalues list unsorted, which breaks closing.</li><li>Closing upvalues in the wrong place (must happen when locals leave scope and at return).</li><li>Upvalue index versus local slot confusion in the compiler.</li></ul>	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::traps
Crafting Interpreters ch 25: which end-of-chapter challenges are worth your time?	Challenge 2 (a fresh variable per loop iteration) is a semantics change; do it on a branch. Challenge 3 (objects from closures) is a fun Lox program to write. Challenge 1 is a performance design question. 	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::challenges
Crafting Interpreters ch 25, Closures: how should you work through it?	Read the whole chapter once before coding. Draw the open upvalues list and the stack for the counter example. Use the test suite heavily; closure bugs are subtle.	cracked.guide cracked.guide::phase::interpreters cracked.guide::chapter::25 cracked.guide::kind::approach
