In Lua table, we can access the corresponding
key
to get
value
value, but cannot match two
table
to perform an operation (such as adding).
So Lua provides a Metatable that allows us to change
table
each behavior is associated with a corresponding meta-method
For example, using a meta-table, we can define how Lua calculates two
table
the addition operation of
a+b
.
When Lua attempts to add two tables, it first checks whether one of the two tables has a meta table, and then checks to see if there is one called
\__add
if it is found, the corresponding value is called
\__add
the corresponding value of a real-time field, such as a function or table, is a “meta-method”.
There are two important functions to deal with meta-tables:
setmetatable(table,metatable)for the specifiedtablesets the meta-table (metatable) if it exists in the meta-table (metatable)\__metatablekey valuesetmetatablewill fail.getmetatable(table)returns the meta-table (metatable) of the object.
The following example shows how to set a meta table on a specified table:
mytable = {} -- regular table
mymetatable = {} -- MetaTable
setmetatable(mytable,mymetatable) -- Set mymetatable to the metatable of mytable
The above code can also be written directly on one line:
mytable = setmetatable({},{})
The following is the meta-table of returned objects:
getmetatable(mytable) -- This will return mymetatable
4.24.1.
\__index
Meta-method #
This is
metatable
the most commonly used key.
When you access it through the key
table
if the key has no value, then Lua will look for the
table
of
metatable
(assuming there is a metatable)
__index
key. If
__index
contains a table in which Lua looks for the corresponding key.
We can use it again.
lua
the command enters interactive mode to view:
$ lua
Lua 5.3.0 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> other = { foo = 3 }
> t = setmetatable({}, { \__index = other })
> t.foo
3
> t.bar
nil
If
__index
include a function
Lua
that function will be called.
table
and keys are passed to the function as arguments.
The The output result of the instance is: Instance resolution: In In Judge whether there is a meta-table. Meta method to see if the parameter of the “key2” key is passed (
\__index
meta-method checks whether the element in the table exists. If it does not, the return result is nil;. If it does, the
\__index
returns the result.Example #
mytable = setmetatable({key1 = "value1"}, {
\__index = function(mytable, key)
if key == "key2" then
return "metatablevalue"
else
return nil
end
end
})
print(mytable.key1,mytable.key2)
value1 metatablevalue
mytable
table is assigned to
{key1
=
"value1"}
.
mytable
meta-table is set, and the meta-method is
\__index
.
mytable
look up in the table
key1
if found, the element is returned, and if it is not found, continue.
mytable
look up in the table
key2
if found, return
metatablevalue
if you can’t find it, go on.
__index
method, if
__index
method is a function, the function is called.
mytable.key2
set), return “metatablevalue” if the “key2” parameter is passed, otherwise
mytable
corresponding key value.