In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to use the Lua meta table metatable. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Lua meta table metatable.
The "_ _ add" meta-method (metamethod) to calculate the union of two Lua arrays, or overload the "_ _ index" method to define our own Hash function. Lua provides two very important methods for dealing with meta-tables, as follows:
Setmetatable (table, metatable): this method is used to set a meta table for a table.
Getmetatable (table): this method is used to get the meta-table object of the table.
_ _ index
When you access table through a key, if the key has no value, then Lua will look for the _ _ index key in the metatable (assuming metatable) of the table. If _ _ index contains a table, Lua looks for the corresponding key in the table.
Tab = {"a", "b", "c"} tab = setmetatable (tab, {_ index = function (tab, key) if key > = 4 then return "d" endend}) print (tab [3]) print (tab [4])
Print the result
Cd
The rules for Lua to find a table element are actually the following three steps:
1. Look up in the table, if found, return the element, and continue if you can't find it.
two。 Determine whether the table has a meta-table, and if there is no meta-table, return nil, and the meta-table continues.
3. Determine whether the meta-table has a _ _ index method. If the _ _ index method is nil, it returns nil;. If the _ _ index method is a table, repeat 1, 2, 3. If the _ _ index method is a function, the return value of the function is returned.
_ _ tostring
_ _ tostring can modify table to convert to string
Local version = {major = 1, minor = 2, patch = 0} print (version) version = setmetatable (version, {_ tostring = function (t) return string.format ("% d.%d.%d", t.major, t.minor, t.patch) end}) print (version)
The output is shown below. If the meta-method is not set, only the address of the table will be output. By redefining the _ _ tostring method of version with setmetatable, you can print the publication number: 1.2.0.
Table: 0x067193201.2.0
Combined with _ _ index meta-function
Local version = {major = 1} version = setmetatable (version, {_ _ tostring = function (t) return string.format ("% d.%d.%d", t.major, t.minor, t.patch) end, _ _ index = function (tMagnekey) return 0 end}) print (version)
Output result
_ _ call
_ _ call can be used by table as a function
_ _ newindex
The output result shows that the newKey index value in the table is still empty. It looks like an assignment operation, but in fact, it only calls the _ _ newIndex meta-method and does not change the elements in t.
At this point, I believe you have a deeper understanding of "how to use Lua meta table metatable". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.