Detailed meta-table concepts in LUA

Source: Internet
Author: User
Tags lua

This article mainly introduces the meta table in Lua, which is the basic knowledge in Lua's introductory learning, and needs friends to refer to the

Wenzu is a table that helps to change the behavior of the table following the help it connects to a key set and related meta methods. These meta methods are powerful LUA features, such as:

Change/Add functionality to the operator table

View Metatables when the key is not available using tables in the __index meta table.

There are indications that there are two important methods in dealing with Metatables which include the use of

Setmetatable (table,metatable): This method is used to set a table for the meta table.

Getmetatable (table): This method is used to get the table's meta table.

Let's take a look at how to set up a table as another meta table. It looks like the following.

The code is as follows:

MyTable = {}

mymetatable = {}

Setmetatable (mytable,mymetatable)

The above code can be represented in a single row as shown below.

The code is as follows:

MyTable = setmetatable ({},{})

__index

Wenzu Lookup meta table, it does not provide a simple example in the table below.

The code is as follows:

MyTable = setmetatable ({key1 = "value1"}, {

__index = function (MyTable, key)

If key = = "Key2" Then

Return "Metatablevalue"

Else

return Mytable[key]

End

End

})

Print (Mytable.key1,mytable.key2)

When we run the above program, we get the output below.

The code is as follows:

Value1 Metatablevalue

Let's explain what happened in the above example steps,

The table mytable here {key1 = "value1"}.

Wenzu set to MyTable contains a function __index we call it the Meta method.

The Meta method does look up the index "Key2" a simple job, if found, returns "Metatablevalue", otherwise the value of the corresponding MyTable index is returned.

We can have a simplified version of the above program, as shown below.

The code is as follows:

MyTable = setmetatable ({key1 = "value1"}, {__index = {Key2 = "Metatablevalue"}})

Print (Mytable.key1,mytable.key2)

__newindex

When we add __newindex to the meta table, if the key is not available in the table, the behavior of the new key is defined by the method of relaying. A simple example where the Meta table index is not in the primary table can be set as follows.

The code is as follows:

mymetatable = {}

MyTable = setmetatable ({key1 = "value1"}, {__newindex = mymetatable})

Print (Mytable.key1)

Mytable.newkey = "New value 2"

Print (Mytable.newkey,mymetatable.newkey)

Mytable.key1 = "New value 1"

Print (Mytable.key1,mymetatable.newkey1)

When you run the above program, you will get the following output.

The code is as follows:

Value1

Nil New value 2

New value 1 Nil

Can be seen in the above program, if a key exists in the main table, it just updates it. When a key is not available in maintable, it adds a key metatable.

Another example of this update with the same table as the Rawset function is shown below.

The code is as follows:

MyTable = setmetatable ({key1 = "value1"}, {

__newindex = function (MyTable, key, value)

Rawset (MyTable, Key, "" ". Value ... "" ")

End

})

Mytable.key1 = "New Value"

Mytable.key2 = 4

Print (Mytable.key1,mytable.key2)

When we run the above program, we get the output below.

The code is as follows:

New Value "4"

Rawset set values instead of using the meta table __newindex. Also has the Rawget, obtains the value, does not need to use the __index.

The behavior of the table join operator

A simple example combining the two tables using the + operator is shown below.

The code is as follows:

MyTable = Setmetatable ({1, 2, 3}, {

__add = function (MyTable, newtable)

For i = 1, TABLE.MAXN (newtable) do

Table.insert (MyTable, TABLE.MAXN (mytable) +1,newtable[i])

End

Return mytable

End

})

secondtable = {4,5,6}

MyTable = mytable + secondtable

For k,v in Ipairs (mytable) do

Print (K,V)

End

When we run the above program, we get the following output

The code is as follows:

1 1

2 2

3 3

4 4

5 5

6 6

The __add key contains the operator + behavior added to the meta table. The key of the table and the corresponding operator are shown below.

__call

Completes the add behavior of the method call, using the __call declaration. A simple example that returns the sum of the main table of values and the pass-through table.

The code is as follows:

MyTable = setmetatable ({10}, {

__call = function (MyTable, newtable)

sum = 0

For i = 1, TABLE.MAXN (mytable) do

sum = sum + mytable[i]

End

For i = 1, TABLE.MAXN (newtable) do

sum = sum + newtable[i]

End

return sum

End

})

newtable = {10,20,30}

Print (MyTable (newtable))

When we run the above program, we get the output below.

The code is as follows:

70

__tostring

To change the behavior of a print statement, you can use the __tostring method. A simple example is shown below.

The code is as follows:

MyTable = setmetatable ({10, 20, 30}, {

__tostring = function (mytable)

sum = 0

For K, V-pairs (mytable) do

sum = sum + V

End

Return "The sum of values in". Sum

End

})

Print (mytable)

When we run the above program, we get the output below.

The code is as follows:

The sum of values in the table is 60

If you know that the metadata table is fully functional, you can really perform a lot of operations, which will be very complicated without using it. So try to get the job done using metatables in the Meta table to provide different options as a sample explanation, or you can create your own samples.

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.