0Pricing
Lua Academy · Lesson

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

All lessons in this course

  1. What Are Upvalues?
  2. Shared Upvalues Between Closures
  3. Factory Functions and Generators
  4. Memoization with Closures
← Back to Lua Academy