fwiw, for those whose eyes gloss over when they start seeing words like “closure”, “environment”, “upvalue”, etc…
an upvalue is a variable that has been bound to the environment of a function.
great, but what the heck does THAT mean?? :D ie, the answer is more confusing than the question, right?
functions in Lua are first-class citizens, and may “travel” outside of the scope in which they were defined. in order to continue working outside of their defining scope, they need to retain their original environment. this combination of a function with its environment is called a closure. all functions in Lua are closures, though they may not necessarily need/use their environment, because they may not actually reference any variables outside of their local scope. any variables from outer scopes that ARE referenced and DO need to be bound into the closure environment are called upvalues.
example: ponder the following code, and once you “get” how ‘n’ is “remembered” then you’ll have a workable understanding
print("at this point 'n' does not exist", n) local function makeSquaringFunction(n) print("at this point 'n' is a local of function makeSquaringFunction", n) return function() print("at this point 'n' is an upvalue of the returned squaring function", n) return n\*n end end print("at this point 'n' does not exist (again)", n) local threeSquared = makeSquaringFunction(3) print("at this point 'n' does not exist (still)", n) print("three squared = ", threeSquared()) -- WHAT?!?! -- ie, how can it possibly still "know" that n = 3?!?! that seems like "voodoo"!!!
hth