I know it is in the manual, but seems that I dont clearly underestand it. So
can somebody explain me what are upvalues and closures?
Roger D. Vargas
Linux user: 180787
* Tanto si piensas que puedes, como si piensas que no puedes, tienes razón *
Prior to Lua 5.0, Lua didn't have lexically scoped variables. Instead
Lua had upvalues which are just read-only values copied from local
variables and stored within the function. The same example in Lua 4
requires the extra %
$ cat closure.lua
local name = 'bob'
-- cannot access name directly from here on,
-- but print_name has a read-only copy of name's value
$ bin/lua -v closure.lua
Lua 4.0.1 Copyright (C) 1994-2000 TeCGraf, PUC-Rio
The only change in the C API between Lua 5 and 4 -- as far as upvalues
are concerned -- is the upvalues for C-closures are at virtual stack
indexes given by lua_upvalueindex(i) instead of being pushed on the
stack after the function arguments. Also you can now access and alter
the upvalues with lua_getupvalue and lua_setupvalue.