Retriable in Lua

Source: Internet
Author: User

What is resumable?

 

The concept of resumable in Lua is translated as a table in China. the meta table provides a public portal for defining the default behavior of any object (value) in Lua. like Operator overloading or method overloading in many oo languages. metatable 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
A = {5, 6}
B = {7, 8}
-- Use C for retriable
C = {}
-- Redefinition of addition operations
C. _ add = function (OP1, OP2)
For _, item in ipairs (OP2) Do
Table. insert (OP1, item)
End
Return OP1
End
-- Set the retriable of a to C
Setretriable (a, c)
-- D now looks like {5, 6, 7, 8}
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.

Actions defined in retriable

Add, sub, Mul, Div, Mod, pow, UNM, Concat, Len, EQ, LT, le, tostring, GC, index, newindex, call...

In Lua, any value has a retriable, and different values can have different retriable and share the same retriable. However, in the functions provided by Lua, you are not allowed to change the retriable of any other type value except the table type value unless you use C extension or other libraries. setretriable and getretriable are the only methods used to operate a table.

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.

Local bird = {canfly = true}

Function bird: New ()
Local B = {}
Setretriable (B, self)
Self. _ Index = self
Return B
End

Local ostrich = bird: New () -- bird. canfly is true, ostrich. canfly is true
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 implement dynamic proxy and AOP. features like this are really simple in Lua, although there are a lot of restrictions, however, 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 () using a function for table A, which triggers _ call. For another example, see my other article.ArticleImplement a C #-like event mechanism in Lua
A = {}
Function A: func ()
Print ("simonw ")
End
C = {}
C. _ call = function (T ,)
Print ("START ")
T. func ()
Print ("end ")
End
Setretriable (a, c)
A ()
--[[
Start
Simonw
End
]

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 in Lua and Lua reference manuals.

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.