Before introducing LUA's data types, it was also mentioned that the LUA function is a "first class value (first-class value)." It can:
stored in variables or table (such as modules and object-oriented implementations)
Copy Code code as follows:
t = {p = print}
T.P ("Just a test!")
Passed as an argument (also called a higher-order function (Higher-order function)) to another function call
Copy Code code as follows:
T = {2, 3, 1, 5, 4}
Table.sort (t, function (a, B) return (a > B) end)
As the return value of another function
Copy Code code as follows:
function fun1 (x) return fun2 (x) end
The feature of the "First class value" in Lua makes it a flexible, resilient data type, while also allowing it to derive some special powerful language mechanisms:
Closure (closure)
A function in Lua is the first class value with a lexical scope (lexical scoping), or the scope of a function variable, where a variable of a function has a certain scope of utility, and a variable can be visible or accessed only within a certain range.
For example, the following code:
Copy Code code as follows:
function count ()
Local UV = 0
Local function Retfun ()
UV = UV + 1
Print (UV)
End
Return Retfun
End
The function retfun is defined in the function count, where the function retfun can be thought of as an inline (inner) function of the count of functions, and the function count as the outsourced (enclosing) function of the function Retfun. Inline functions can access all local variables that the outsourced function has created, which is the lexical scope described above, and these local variables (such as the variable UV above) are referred to as external local variables of the inline function (external-variable) or upvalue.
Execute function Count:
Copy Code code as follows:
C1 = COUNT ()
C1 ()--Output 1
C1 ()--Output 2
Two times above call C1, you will see Output 1 and 2 respectively.
For a local variable UV in a function count, its lifecycle should end when "C1 = Count ()" is executed, but since it has become the external local variable upvalue of the inline function retfun, the returned inline function Retfun Upvalue The value of the UV is saved so that the value can be printed correctly.
This local variable will continue to exist after the function returns, and the returned function can normally invoke that local variable to perform its logical operation independently, which is called the closure in Lua (closure)
The closure is an independent entity, which can then assign the function count to a variable and then perform a look at the output effect:
Copy Code code as follows:
C2 = count ()
C2 ()--Output 1
C1 and C2 are the same function bodies, but the output value is not the same! This is mainly because closures are composed of references to the corresponding function prototypes and upvalue of external local variables. When the calling function causes the Upvalue value to be changed, this will only change the corresponding closure of the Upvalue value, will not affect the other closure of the Upvalue value, so C1 is called 2 times after the external local variable UV value is 2, and the newly created C2 initial external local variable UV is 0 , it will be 1 after being invoked.