Meta method in Lua __newindex detailed _lua

Source: Internet
Author: User
Tags lua

OK, I write the progress of the article is not enough to read the progress of the book, a few simple pieces of text will suffice me to nag an article.

Today we continue to say the Meta method, which is somewhat similar to the __index __newindex method.

1. Query and update

In the previous article we introduced the __index meta method, which concludes that the __index method is used to process fields that do not exist in the call table.

Notice that the word "call" is just called, not assigned.

What if we want to assign a value to a field that doesn't exist in the table? (Jor: Just, direct assignment Ah!) )

Yes, we can directly assign value, will not error.

The question is, what if I want to monitor the operation? If someone wants to assign a value to a field that doesn't exist on the table, I want to do some extra processing?

It's time to use __newindex.

Everyone should remember this sentence: __index is used for queries, __newindex for updates.

Wait until the chaos, the first contact, there may be confusion.

2. Take a look at the normal value of the assignment

Let's take a look at the normal assignment, such as code:

Copy Code code as follows:

Local Smartman = {
Name = "None",
Money = 9000000,

SayHello = function ()
Print ("Hello everyone, I am smart Hao.") ");
End
}

Local T1 = {};

Local MT = {
__index = Smartman,
}

Setmetatable (t1, MT);

T1.sayhello = function ()
Print ("en");
End

T1.sayhello ();

This is the example used in the previous article, a simulation of the structure of inheritance.
To analyze, MT as the T1, set __index as Smartman.
So, when we call a field that doesn't exist in T1, we automatically look it up in the Smartman.
For example, we call the T1.sayhello (), and naturally we can find the corresponding function.

Let's take a look at the output:

Copy Code code as follows:

[Lua-print] en

We call the T1 sayhello field, T1 does not exist (although the Smartman field can be found in __index way).

However, this does not affect the assignment of this field and then call T1.sayhello (), which is found to be successful.

This is the same as we did in the past, doing normal assignment to table, regardless of whether the table itself exists in this field.

3. Monitoring assigned value

Well, we've tried the normal situation, if we want to monitor the assignment of the table?
For fields that do not exist, we do not need to be assigned a value? Want to make a read-only table?

If you have these ideas, then welcome to the number below the screen, the first 10 to enter the gift value. (Jor: Stop!) )
So, if you have these ideas, please look at the following code:

Copy Code code as follows:

Local Smartman = {
Name = "None",
Money = 9000000,

SayHello = function ()
Print ("Hello everyone, I am smart Hao.") ");
End
}

Local T1 = {};

Local MT = {
__index = Smartman,
__newindex = function (table, key, value)
Print (key.. "The field does not exist, do not try to assign it a value!" ");
End
}

Setmetatable (t1, MT);

T1.sayhello = function ()
Print ("en");
End
T1.sayhello ();

Notice the Mt Meta table, we add a __newindex to it.
Run the code and the output is as follows:

Copy Code code as follows:

[Lua-print] SayHello field does not exist, do not try to assign it a value!
[Lua-print] Everybody good, I am the smart Hao.

Obviously, the SayHello field assignment failed because the __newindex method was invoked instead of the assignment operation when the SayHello field was assigned a value.

(Jor: Why?) Does the SayHello field not exist? Why does it say that it doesn't exist? )

Here is a place to note that there is indeed no sayhello field in the T1, it is simply because there is a meta table, and the value of the __index method in the Metamodel is smartman this table.

Thus, in T1 can not find the SayHello field, to Smartman to find.

But, in fact, T1 does not exist in the SayHello field, do not know if you can get around it?

Therefore, when trying to assign a value to T1 's SayHello field, Lua determines that the SayHello field does not exist, so it invokes the __newindex element method in the Metamodel.

When the __newindex method is invoked, it passes in 3 parameters: The table itself, the field name, and the value you want to assign.

4. GE beat cattle, assign a value to a table to another table

As with __index, the __newindex method can also give a table value.
This is a bit of an interesting situation, first look at the code:

Copy Code code as follows:

Local Smartman = {
Name = "None",
}

Local other = {
name = "Hello everyone, I am a very innocent table"
}

Local T1 = {};

Local MT = {
__index = Smartman,
__newindex = Other
}

Setmetatable (t1, MT);

Print ("Other name, before assigning value:"; other.name);
T1.name = "thief";
Print ("Other name, after assignment:" ... other.name);
Print ("T1 's name:" ... t1.name);

This time the code was just about the same, but we added a table for other, and then we took the other as the __newindex value.

So when you assign a value to the T1 name field, something strange happens ...

Let's take a look at the output:

Copy Code code as follows:

[Lua-print] Other's name, before assigning: hello everyone, I'm a very innocent table.
[Lua-print] Other's name, after assignment: the Thief
[Lua-print] T1 's name: none

When the name field of T1 is assigned a value, the Name field of the other is assigned, and the name field of the T1 is not changed.

(actually T1 's name field does not exist, it just finds the Smartman name field by __index, this does not nag.) )

So, when we assign a value to T1 's name, we actually assign the name to the other.

All right, poor other.

5. Summary rules

This is the __newindex rule:

A. If __newindex is a function, this function is called when you assign a value to a field that does not exist for the table.
B. If __newindex is a table, assigning a value to a field that does not exist for the table assigns a value directly to the __newindex table.

6. End

Well, the basic content of the meta table and the meta method basically came to an ending, and then there is an article on the meta table and meta method, but also some of the more fragmented knowledge points.

Later, the meta table and the Meta method are also mentioned, because they are so important.

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.