The closure of Lua

Source: Internet
Author: User

http://blog.csdn.net/ym012/article/details/7208750

Functions can still be defined in the body of the function. Assuming that the function F2 is defined in the function F1, then it is called F2 as an inline (inner) function of F1 and F1 as F2 (enclosing) function

Inline functions can access all the local variables that the outsourced function has created, which is called the word legal realm (lexical scoping)

    function F1 (n)         --functions parameter is also local variable                     f2 ()            print (n)--local variable referencing outsource function         end         return f2      end                  G1 = F1 (1979)      G1 ()--Print out 1979 g2      = F1 (+)      G2 ()--Print out 500  

Why is the function body of G2 and G1 the same (all function body of F1 inline function F2), but the printing value is different? This involves a fairly important concept-closure (closure). In fact, when Lua compiles a function, it generates a prototype for him (prototype), which contains the virtual machine instruction for the function body, the constant value (number, text string, and so on) used by the function, and some debugging information. At run time, whenever Lua executes an expression such as function...end, he creates a new data object containing references to the corresponding function prototypes, a reference to the environment (environment, a table for finding global variables), and a An array of upvalue references, and this data object is called a closure. Thus, the function is a compile-time concept, is static, and the closure is the concept of runtime, is dynamic. The value of G1 and G2 is strictly not a function but a closure, and two different closures, and each closure can retain its own upvalue value, so G1 and G2 will certainly not print the same result.

    function F1 (n)         local function f2 ()            print (n)         end         n = n + ten         return F2      end                  G1 = F1 (1979)      G1 ()--Print out 1989  

The inline function is defined before the N = n + 10 statement, but why does the G1 () print 1989? Upvalue is actually a local variable, and the local variable is stored on the function stack frame (stack frame), so as long as Upvalue has not left its scope, he has been living on the function stack. In this case, the closure will access them through a reference to the Upvalue on the stack, and once upvalue is about to leave its scope (which also means that he is going to disappear from the stack right away), the closure will allocate space for him and save the current value, and later, by pointing to the newly assigned null To access the Upvalue. When execution to F1 (1979) n = n + 10 o'clock, the closure has been created, but N does not leave the scope, so the closure still refers to N on the stack, when the return F2 completes, n is about to end the life, when the closure will be N (already 1989) copied to the space in their own management for future access, But the variables on the stack are first written again before they are written to the new space--n=n+10

If the above code removes F2 's local, change it to:

    function F1 (n)    function f2 ()          print (n)       end       n = n + ten       return F2    end    G1 = F1 (1979)    G1 ()--Print out 1989    F2 ()--print 1989, because F2 is a closure, internally has its own n

Pvalue can also provide a mechanism for data sharing between closures. The following example:

    function Create (n)         local function foo1 ()            print (n)         end         local function Foo2 ()            n = n + ten         end         Return Foo1,foo2      end            f1,f2 = Create (1979)      F1 ()--Print 1979      F2 ()      F1 ()--Print 1989      F2 ()      F1 ()--Print 1999  

The f1,f2 of the two closures are the embedded functions foo1 and Foo2 in Create, while the Foo2 referenced by Foo1 and Upvalue are the same, that is, the local variable n of Create. As I said earlier, after executing the Create call, the closure will copy the value of n on the stack, then whether F1 and F2 have a copy of n respectively? In fact, when Lua discovers that the upvalue of two closures point to the same variable on the current stack, it intelligently generates only one copy, and then lets the two closures share the copy, so that any closure changes to the upvalue will be detected by another. The example above illustrates this very clearly: each call to F2 increases the value of Upvalue by 10, and then F1 prints the updated value. This semantics of upvalue is very valuable, allowing the closure to communicate without relying on global variables, thus greatly improving the reliability of the code.

The closure of Lua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.