syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Lua what is an upvalue?

In Lua an upvalue is a variable in an anonymous function which was created outside the function.
-- ~/ks/b/lua/upvalue.lua

-- An upvalue is a variable.

-- First I create the variable.

-- Then I use the variable inside an anonymous function.

function print_this()
  upv = 'I am upvalue value'
  return function(str)
    str = 'Yes, '
    return str..upv
  end
end

-- To see upv,
-- I cannot just call print_this()
print(print_this())

-- I need to point to it:
print_that = print_this()

-- Then I can call it:
print(print_that())


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me