Analyzing the notes about resumable in Lua

Source: Internet
Author: User

LuaAboutRetriableLearning notes are the content to be introduced in this article. First, let's take a look at what isRetriable, MediumRetriableIn China, this concept is translated as a meta-table. The meta-table is redefined.LuaThe default behavior of any object (value) in provides a public entry, just like Operator overloading or method overloading in many OO languages.RetriableIt can bring us a very flexible programming method.

Specifically, each type of value in Lua has its default operation mode. For example, numbers can be used for addition, subtraction, multiplication, division, and other operations. Strings can be used for connection operations, and functions can be used for call operations, A table can assign values to table items. they all follow the default logic of these operations, and these operations can be changed through retriable. for example, you can define how two tables are added.

Let's take a look at the simplest example and redefine the addition operation for the two tables. in this example, rewrite the _ add field of c and set the retriable OF a to c. When performing the addition operation, lua first checks whether a has a retriable and whether the _ add domain exists in the retriable. If yes, it calls the statement. Otherwise, it checks the condition of B (which is the same as that of ), if no addition operation exists, the default addition operation is called, and the table does not define the default addition operation. An error is reported.

-- Define two tables

 
 
  1. a = {5, 6}  
  2. b = {7, 8} 

-- Use c for retriable

 
 
  1. c = {} 

-- Redefinition of addition operations

 
 
  1. c.__add = function(op1, op2)  
  2.    for _, item in ipairs(op2) do  
  3.       table.insert(op1, item)  
  4.    end  
  5.    return op1  
  6. end 

-- Set the retriable OF a to c

 
 
  1. setmetatable(a, c) 

-- D now looks like {5, 6, 7, 8}

 
 
  1. d = a + b 

With a perceptual knowledge, let's look at the specific features of retriable.

Retriable is not mysterious. It is just a common table. In the table data structure, Lua defines many portals for redefinition of these operations. they all begin with a double underline as the table field, as shown in the example above _ add. when you set the retriable for a value and overwrite the corresponding operation field in the retriable, this operation will trigger the rewrite User-Defined operation. of course, each operation has a method signature for each operation. For example, _ add will pass in two operands on both sides of the plus sign as parameters and require a return value. some people compare this behavior to an event. When xx behavior is triggered, the event custom operation will be activated.

Add, sub, mul, div, mod, pow, unm, concat, len, eq, lt, le, tostring, gc, index, newindex, call...

InLuaEach value in has a retriable, and different values can have different retriable and share the same retriable, but in the functions provided by Lua itself, you are not allowed to change any type value except the table type valueRetriableUnless C extension or other libraries. setretriable and getretriable are the only group of operation table types.RetriableMethod.

Retriable and object-oriented

Lua is a process-oriented language, but it can simulate the appearance of an object through the retriable. the key lies in the _ index field. it provides the index value entry for the table. this is similar to rewriting the indexer in C #. When the table wants to index A value such as table [key], Lua will first look for the key value in the table itself, if the table does not have a retriable with the _ index attribute, Lua searches for the table based on the function logic defined by _ index. if you think about it, isn't this an implementation method for the core idea inheritance in Object-Oriented Systems. there are many ways to implement object-oriented in Lua, But no matter which one can do without _ index.

In this example, I used the method of implementing OO In Programming In Lua to create a Bird object with flying attributes. Other Bird objects are based on this prototype, Ostrich (Ostrich) it is a type of bird, but it cannot fly. obviously, Bird and Ostrich have independent states.

 
 
  1. local Bird = {CanFly = true}  
  2.  
  3. function Bird:New()  
  4.     local b = {}  
  5.     setmetatable(b, self)  
  6.     selfself.__index = self  
  7.     return b  
  8. end  
  9.  
  10. local Ostrich = Bird:New() --Bird.CanFly is true, Ostrich.CanFly is true  
  11. Ostrich.CanFly = false --Bird.CanFly is true, Ostrich.CanFly is false 

_ Newindex corresponds to _ index, which is triggered when the table key is updated. You can use rawset and rawget to perform the key operations on the table to skip the triggering of these events.

Call and Interception

Java and C # require a lot of twists and turns to achieve dynamic proxy and AOP, similar to this function inLuaIt is very simple. Although there are many restrictions, you can still feel the flexibility of Lua. This is the _ call operation, which is triggered when the value is called.

Here, I call a () as a function for table Type a, which triggers _ call. for another application example, see my other article Lua to implement a C #-like event mechanism.

 
 
  1. a = {}  
  2. function a:Func()  
  3.    print("simonw")  
  4. end  
  5. c = {}  
  6. c.__call = function(t, )  
  7.    print("Start")  
  8.    t.Func()  
  9.    print("End")  
  10. end  
  11. setmetatable(a, c)  
  12. a()  
  13. --[[  
  14. Start  
  15. simonw  
  16. End  
  17. ]] 

The examples here are presented In the simplest way, so that you can clearly describe the core. For more information and specific applications, see Programming InLuaAndLuaReference manual.

Summary: AnalysisLuaAboutRetriableThe content of the study notes has been introduced. I hope this article will help you!

Related Article

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.