Because I do not want to squeeze too many knowledge points in an article, so, some small knowledge points are assembled into this article ~
1. Silence skills--rejection of __index and __newindex effects
Although __index and __newindex are very useful features, sometimes we want to simply call the table or assign a value to the table.
So, what do we do? Reset a meta table to table? No, this is a bad idea.
So, the thoughtful LUA gives us a way to invoke the following code:
Copy Code code as follows:
local Smartman = {
& nbsp; name = "None",
}
Local t1 = {
&nbs p; hehe = 123;
};
Local MT = {
__index = Smartm An,
__newindex = function (t, K, v)
& nbsp; print ("Do not assign value!") ");
end
}
; Setmetatable (t1, MT);
print (rawget (T1, "name"));
Print (rawget (T1, "hehe"));
rawset (T1, "name", "thief");
Print (t1.name);
By using the Rawget function, you can ignore the __index effect of the meta table and simply call the fields from the T1.
The first parameter of the Rawget is the table to call, and the second parameter is the field name of the table.
Therefore, calling the T1 name field through Rawget can only return nil, and the Hehe field is invoked to get the value correctly.
Similarly, the Rawset function can ignore the __newindex effect of the meta table and simply assign values to T1.
To see the output:
Copy Code code as follows:
[Lua-print] Nil
[Lua-print] 123
[Lua-print] Thieves
Gets the name field, outputting nil;
Get hehe field, output 123;
After you modify the Name field, export the "thief"
This is equivalent to T1 there is no __index and __newindex element method.
How, this silence skill is very interesting.
2. read-only table
Well, suppose you continue to be a master, you write a very cow function, and then as the master of you, every night to go home to see the film.
So your function has to give to the company those who just graduated from the 30 to maintain the new, so that they work overtime every day until 6:30 in the evening. (Jor: Hello!) Does 6:30 count overtime? )
However, the function of such cattle, can not be these new people casually changed, so, in addition to protecting the table's meta table, you also want to protect the table field.
You want to make sure that these new people aren't going to change the field values of your table.
Yes, this time you can use __index and __newindex to implement the following code:
Copy Code code as follows:
Local function readOnly (t)
Local NewT = {};
Local MT = {
__index = T,
__newindex = function ()
Error ("Don't modify me!") I'm a read-only! ");
End
}
Setmetatable (NewT, MT);
return NewT;
End
Local days = ReadOnly ({"Monday", "Tuesday", "Sunday"});
DAYS[2] = "Where did Wednesday go?" " ;
This may be a bit difficult to understand, let's look at the output first:
Copy Code code as follows:
[Lua-print] LUA ERROR: [string ' Src/main.lua ']:130: [string ' Src/main.lua ']:76: Don't change me! I'm a read-only!
Yes, the table generated by ReadOnly is not assignable.
So, what's the principle? Let's think about it one step at a while:
A. First, ReadOnly creates a new table and then takes the table we pass in as the __index element method.
B. The __newindex is also added to block the assignment operation of fields that do not exist.
The table returned by C.readonly is not our original table, it is an empty table, but it is set up with a new meta table.
D. Start assignment operations on days: days[2] = "Where did Wednesday go?" ” 。
E.days is an empty table, so it does not exist in this field, and therefore, calls the __newindex method, and the assignment fails.
F. If just call days, do not assign values, such as: print (days[2]); You can output the value of the field normally, because there is a __index element method in the element of days. Although there is no 2 in the days of this field, but you can find this field by __index.
All in all, in the end, days become a table that can only be read and cannot be assigned to operations.
(Jor: What if I use the Rawset function?) Doesn't that break your limit? )
Cough, let's go on.
3. End
Finally ended, these days almost all in writing articles, not how to read, but I will continue to write articles ~
After reading the book does not record, always feel not deep enough ~ and write the article mood is very good ~