[Conversion] Lua language basic summary (5) -- closure

Source: Internet
Author: User

Preface

The concept of closure exists in many languages. Here, I will analyze and summarize the concept of closure in Lua. I hope it will help you learn about Lua.

 

What is a closure?

A closure is a very important concept in Lua. A closure is a combination of a function and its reference environment. Let's take a look at the Code:

1234567891011 function newCounter()     local i = 0     return Function () -- anonymous Function          i = i + 1          return i     endend c1 = newCounter()print(c1()) print(c1())

 

Based on the closure concept and the code above, we can talk about this concept. Closure = Function + reference environment. The newcounter function in the above Code returns a function, and the returned anonymous function is the function in the component of the closure; the referenced environment is the environment where variable I is located. Actually, the closure is just like a function in form and expression, but it is not actually a function. We all know that a function is a combination of some executable statements, these code statements are determined after the function is defined and will not change when executed. Therefore, the function has only one instance. The closure can have multiple instances at runtime. Different reference environments and the same function combination can generate different instances, just like the same class code, you can create different class instances. When reading other people's articles, we can see that subfunctions can use local variables in the parent function. This behavior is called a closure! This statement illustrates a form of closure, allowing us to better understand what a closure is. As for the deep closure, let's continue.

 

Look at the closure.

First, create a function in Lua, just like defining a normal type value. As mentioned in the previous article, there is no difference between a function in Lua and a normal type. A function in Lua is a so-called "first-class value". It can be stored in a variable or data structure and passed to another function as a parameter. It can be the return value of a function, it can also be created during running. The function in Lua is such a "thing", which is flexible. Is the concept of "non-local variables" mentioned in Lua functions? This is a very important concept. It can be understood as not a variable defined in the scope of local action. At the same time, it is not a global variable, that is, what we call upvalue, the existence of such a variable makes the closure in Lua complete. These variables are mainly used in nested functions and anonymous functions. We all know that You can redefine the function in Lua's function, that is, embedded function. Embedded functions can access all the local variables created by external functions, these variables are called the upvalue of the embedded function. upvalue actually refers to variables rather than values. These variables can be shared among internal functions, such as the following code:

123456789101112131415161718192021222324 function Fun1()     local iVal = 10          -- upvalue     Function innerfunc1 () -- embedded Function          print(iVal)          --     end      Function innerfunc2 () -- embedded Function          iVal = iVal + 10     end      return InnerFunc1, InnerFunc2end -- Assign a function to the variable. At this time, variable A is bound to the innerfunc1 and function B is bound to the innerfunc2 function.local a, b = Fun1() -- Calla()          -->10 -- Call BB () --> modified upvalue ival in function B. -- Call a to print the modified upvaluea()          -->20

The above simple code verifies that the built-in functions share upvalue, just like the member functions in the C ++ class can access and modify member variables.

 

Use closures

We can see that the closure is a combination of data and behavior, just like the class in C ++, so that the closure has good abstract capabilities. In some cases, we need to remember the status of data after a call is completed, just like static variables in C ++. After each call is completed, static variables are not cleared. You can use closure to complete this function. In the next blog, I will talk about using closure to complete the iterator function.

 

Summary

Closure is a very important concept. It can be understood or hard to understand. Simply put, closure is an embedded function and upvalue that can be accessed correctly. Many times, we understand this truth but do not use it. Therefore, we need to read more code, participate in more projects, and accumulate more project experience, to enrich your experience. At that time, the level of understanding will go up.

 

Http://cn.cocos2d-x.org/tutorial/show? Id = 1078

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.