end
print(x) --> 11
end
print(x) --> 10 (the global one)
<
Notice that, in a declaration like `local x = x`, the new `x` being declared is
not in scope yet, and so the second `x` refers to the outside variable.
*lua-upvalue*
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.
Notice that each execution of a local statement defines new local variables.
Consider the following example:
>lua
a = {}
local x = 20
for i=1,10 do
local y = 0
a[i] = function () y=y+1; return x+y end
end
<
The loop creates ten closures (that is, ten instances of the anonymous
function). Each of these closures uses a different `y` variable, while all of
them share the same `x`.
==============================================================================
2.7 Error Handling *lua-errors*
Because Lua is an embedded extension language, all Lua actions start from
C code in the host program calling a function from the Lua library (see
|lua_pcall()|). Whenever an error occurs during Lua compilation or
execution, control returns to C, which can take appropriate measures (such as
print an error message).
Lua code can explicitly generate an error by calling the `error` function (see
|error()|). If you need to catch errors in Lua, you can use the `pcall`
function (see |pcall()|).
==============================================================================
2.8 Metatables *metatable* *lua-metatable*
Every value in Lua may have a metatable. This metatable is an ordinary Lua
table that defines the behavior of the original table and userdata under
certain special operations. You can change several aspects of the behavior of
an object by setting specific fields in its metatable. For instance, when a
non-numeric value is the operand of an addition, Lua checks for a function in
the field `"__add"` in its metatable. If it finds one, Lua calls that function
to perform the addition.
We call the keys in a metatable events and the values metamethods. In the
previous example, the event is "add" and the metamethod is the function that
performs the addition.
You can query the metatable of any value through the `getmetatable` function
(see |getmetatable()|).
You can replace the metatable of tables through the `setmetatable` function (see
|setmetatable()|). You cannot change the metatable of other types from Lua
(except using the debug library); you must use the C API for that.
Tables and userdata have individual metatables (although multiple tables and
userdata can share a same table as their metatable); values of all other types
share one single metatable per type. So, there is one single metatable for all
numbers, and for all strings, etc.
A metatable may control how an object behaves in arithmetic operations, order
comparisons, concatenation, length operation, and indexing. A metatable can
also define a function to be called when a userdata is garbage collected. For
each of those operations Lua associates a specific key called an event. When
Lua performs one of those operations over a value, it checks whether this
value has a metatable with the corresponding event. If so, the value
associated with that key (the metamethod) controls how Lua will perform the
operation.
Metatables control the operations listed next. Each operation is identified by
its corresponding name. The key for each operation is a string with its name
prefixed by two underscores, `__`; for instance, the key for operation "add"
is the string "__add". The semantics of these operations is better explained
by a Lua function describing how the interpreter executes that operation.
The code shown