Bad example. XML is the wire format in XMPP RFCs, not a file format.
function create()
local a,b,c,d,e,f,g,h,i,j;
return function() print(a,b,c,d,e,f,g,h,i,j); end
end
function array_get(f, index)
local k,v = debug.getupvalue(f, index);
return v;
end
function array_set(f, index, value)
debug.setupvalue(f, index, value);
end
function map_get(f, key)
for i=1,math.huge do
local k,v = debug.getupvalue(f, i);
if k == key then return v; end
if k == nil then return nil; end
end
end
function map_set(f, key, value)
for i=1,math.huge do
local k,v = debug.getupvalue(f, i);
if k == nil then return; end
if k == key then
debug.setupvalue(f, i, value);
return;
end
end
end
function test()
local f = create();
array_set(f, 1, "one")
assert(array_get(f, 1) == "one");
map_set(f, "b", "something");
assert(map_get(f, "b") == "something");
print("Success!")
end
test();
If table creation was disallowed, you could use functions in place of tables. And you could repurpose an existing table as the function type's metatable, allowing syntax like func.x=func[y] Because of the lexical scoping rules, local variables can
be freely accessed by functions defined inside their scope.
A local variable used by an inner function is called an
upvalue, or external local variable, inside the inner
function.
i.e., the external local variables the closure is closed on. And for C functions made available to Lua scripts: When a C function is created, it is possible to associate
some values with it, thus creating a C closure (see
lua_pushcclosure); these values are called upvalues and are
accessible to the function whenever it is called.
- http://www.lua.org/manual/5.2/manual.html