What Are Upvalues?
Understand how closures capture variables as upvalues from enclosing scopes.
Closures Recap
A closure is a function bundled with references to variables from its enclosing scope. Those captured variables are called upvalues.
Upvalue Example
The inner function captures count from the outer function's scope.
local function makeCounter()
local count = 0
return function()
count = count + 1
return count
end
end
local next = makeCounter()
print(next()) -- 1
print(next()) -- 2