A meta-table (metatable), meta-method (Metamethod) in Lua is detailed

Source: Internet
Author: User
Tags lua mul

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

1. Know why 1 + 1 = 2?

Why is it equal to 2 in Lua? (Jor: Is it not equal to 2 other places other than Lua?) )
Why do numbers and numbers add up to be legal, and why does table and table add up to an error? Have you ever thought about these questions?

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

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

In Lua, the secret to defining these rules is the meta-table and meta-methods.

2. Meta-Methods

Meta-method, which sounds esoteric, is actually a function of special operations between lattice-type variables.
For example, the addition of numbers, it may be just a function.

For example: In the bottom, it may be this: Add (1, 1). The Add function is used to calculate the result of adding the two numbers to each other.

Again such as: 10X15, it may be this: Mul (10, 15). The MUL function returns the result of multiplying two numbers.
(Maybe this is not a good example, but that's what it means ~)

And finally, what if it's a two table?

Copy CodeThe code is as follows:
Local T1 = {};
Local t2 = {};
T1 + T2;


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

3. Meta-table

MetaTable itself has no effect, it is used to store a meta-method of a table.

Each value in Lua has or can have a meta-table, and table and UserData can have separate meta tables.
However, other types of values can only share the meta-table to which their type belongs, for example, numbers, and all numbers share a single meta-table.

4. Changing the Rules

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

Copy CodeThe code is as follows:
Local T1 = {};
Local t2 = {};
Local result = t1 + t2;

Direct operation of the positive 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 a meta-method.

For example, the meta-method of the addition operation is: __add, which is specified by Lua.

As long as a value of the meta-table contains __add this meta-method, you can use the + number for the operation.

The following code:

Copy CodeThe code is as follows:
--Create a meta-table
Local MT = {};
Mt.__add = function (t1, T2)
Print ("The result of the addition of two table is ... What a psycho! What is there to add to table? ");
End
Local T1 = {};
Local t2 = {};

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

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

First, a table variable, MT, is created to add an element to the table __add, which has the status of being a meta-table.

Then create two new table variables and use the Setmetatable function to set a new meta-table for table, at which point the two table variables are taken as a meta-table by Mt.

Finally, adding operations to T1 and T2, the __add meta-method is looked up from the meta-table, and if found, the meta-method is called to add the two variables.

The output results are as follows:

Copy CodeThe code is as follows:
[Lua-print] The result of the addition of two table is ... What a psycho! What is there to add to table?


It's that simple, and the meta-table and meta-methods actually set some operations on the values in Lua, such as addition, subtraction, and so on, so that we can customize these operations.

However, there are a few points to pay special attention to:
A. When creating a new table variable, it is a non-existent meta-table (you can use the Getmetatable function to get a meta-table of an object to know if the object has a meta-table)

B. In Lua, you can only set the table's meta-table, other types of values of the meta-table, only through the C code to complete

A meta-table (metatable), meta-method (Metamethod) in Lua is detailed

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.