Meta-table (metatable), meta-method (Metamethod) in Lua _lua

Source: Internet
Author: User
Tags lua mul

Finally to the actual use of the content is often--meta table and meta method.

When you first see these two things, you may find it very esoteric, but it is very well understood, although it may actually be really esoteric. (Jor: Stop!) Roll thick. )

1. Do you know why 1 + 1 = 2?

Why is 1+1 equal to 2 in Lua? (Jor: Is it not equal to 2 other places except Lua?) )
Why is the addition of numbers and numbers legal, and why does the table and table add an error? Have you thought about these questions?

Yes, the rules, it's all just rules, Lua prescribes that numbers can be subtraction, but not between the table.

This is because, in the human world, there is no concept of table and table addition.

In Lua, the secret of these rules is the meta table and the Meta method.

2. Meta method

Meta method, sounds very esoteric, in fact it is a lattice type variable between the special operation of the function.
For example, the addition of numbers, it may be just a function.

For example: 1+1, at the bottom, it might be like this: Add (1, 1). The Add function is used to calculate the result of the addition of two digits.

Another example: 10x15, it may be like this: Mul (10, 15). The MUL function returns the result of multiplying two numbers.
(Maybe this is not the right example, but that's what it means ~)

Finally, what if it's two table?

Copy Code code as follows:

Local T1 = {};
Local t2 = {};
T1 + T2;

It may be like this:????
Yes, there is no function in Lua that can calculate the addition of two table, that is, there is no such meta method.

3. Meta table

The Wenzu itself has no effect, it is a table for storing the meta method.

Each of the values in Lua has or can have a meta table, and table and UserData can all have separate meta tables.
However, other types of values can share only the meta tables whose types belong, such as numbers, and all numbers share a single meta table.

4. Changing the Rules

If we say, we just want to add two table?
Try the following code:

Copy Code code as follows:

Local T1 = {};
Local t2 = {};
Local result = t1 + t2;

Direct operation is definitely an error.

So, to meet our needs, LUA allows us to modify the meta table.

A meta table is actually a table value, so we just need to create a new table and add the Meta method.

For example, the Meta method for addition operations is: __add, which is a LUA rule.

As long as the meta table of a value contains the __add method, it can be calculated using the + number.

The following code:

Copy Code code as follows:

--Create a meta table
Local MT = {};
Mt.__add = function (t1, T2)
The result of print ("two table added) is ... What a psycho! What does table have to add together Ah! ");
End
Local T1 = {};
Local t2 = {};

--Set up a new Wenzu for two table
Setmetatable (t1, MT);
Setmetatable (T2, MT);

--Perform addition operations
Local result = t1 + t2;

First create a table variable MT, add an element to this table __add, this table has the qualification as a meta table.

Then create two new table variables, and use the Setmetatable function to set a new meta table for the table, at which point the two table variables take MT as the meta table.

Finally, the addition of T1 and T2, then will look for the __add element method from the meta table, if found, call this meta method to add operations on two variables.

The output results are as follows:

Copy Code code as follows:

[Lua-print] The result of adding two table is ... What a psycho! What does table have to add together Ah!

It is so simple that the meta table and the meta method actually set some actions for the values in Lua, such as addition and subtraction, so that we can customize these operations.

However, there are a few points to pay special attention to:
A. When you create a new table variable, it does not have a meta table (you can use the Getmetatable function to get an object's meta table to know if the object has a meta table).

B. In Lua, you can only set the meta table for the table, the meta table for other types of values, only through C code

5. End

Well, actually I think this article is more messy, not very satisfied.

I want to introduce the meta table and the Meta method in a simpler way, but it seems to backfire.

It doesn't matter, I hope you can barely understand ~

This article is just an overview of the meta table and the Meta method, and will be covered in more detail later.

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.