I. Preface
Lua was invented by Robert ierusalimschy, Waldemar Celes and Luiz Henrique De Figueiredo in 1993. Lua was designed to easily embed the host language, enhances system scalability and customization. Lua has more than 20 thousand lines of source code, which is very simple and compact. In the current Script Engine, Lua is the fastest, which is also the top 20 that Lua entered the programming language, nowadays, it has been widely used in the gaming industry. These articles will discuss several important features of Lua.
The Type System of a language is the most fundamental feature. Therefore, this article first talks about the meta-table and metadata methods that are most closely related to the type system of Lua. As a lightweight language, Lua's core is very simple. It has eight basic types: nil, Boolean, number, String, userdata, function, thread, and table, table is a unique data structure and the most important type in Lua. It can be used as the basis of other data structures, such as arrays, linked lists, queues, and collections. What's more powerful is that Lua also provides table user-defined operations. In object-oriented languages such as C ++, the operational behavior of classes is determined by the member functions. In Lua, the metadata method is the "member function" of the table, which provides special operation behavior for different tables. The metadata table is a collection of metadata methods. Through meta-tables and meta-methods, tables can directly implement class, inheritance, and other object-oriented features.
II. Introduction to the meta-Table Method
In Lua, each value actually has a metadata table, but each table and userdata can have its own proprietary metadata table. (userdata is the data structure in the host and can be used in the host language, in order to limit the excessive use of meta-tables, you cannot directly set them in the Lua script. You need to create them using lua_setmetatable, which is not discussed here ), other types of pre-defined operations are in a shared meta-table. The new table does not have a meta-table by default. You must set and query the meta-table through setretriable and getretriable.
t = {}assert(getmetatable(t)== nil) t1 = {}setmetatable(t, t1)assert(getmetatable(t) == t1)
The functions defined in the meta-table are meta-methods. The meta-Methods of the table are classified into the arithmetic class, relational class, library definition, and metadata class meta-methods.
1. Arithmetic meta Method
Lua's arithmetic meta-methods all have corresponding field names, including _ add, _ Mul, __sub, _ Div, __mod, and _ pow, the following example shows how to define addition operations for two tables,
a = { "a1", "a2","a3" }b = { "b1", "b2", "b3" }meta = { }meta.__add = function(t1, t2) t = { } for k, v in ipairs(t1) do table.insert(t, v) end for k, v in ipairs(t2) do table.insert(t, v) end return tendsetmetatable(a, meta)c = a+bfor _,v in ipairs(c) do print(v)end
In the code above, you only need to set a meta table for table A, and table B can run normally without a meta table, which is related to the order in which Lua searches for the meta table. Lua first looks for the first table. If there is a metadata table and the _ add method exists, this method is called, regardless of whether the second table has a metadata table; otherwise, check whether the second table has the _ add metadata method. If yes, call the metadata method of the second table. If no metadata method is available, an error is thrown.
2. Link meta Method
The meta-Method of The Link class can only be equal to _ eq, less than _ lt, and less than or equal to _ LE. The other three are automatically converted, for example, A> B is automatically converted to B <.
a = { "a1", "a2" }b = { "b1", "b2", "b3" }meta = { }meta.__le = function(a, b) for k in pairs(a) do if not b[k] then return false end end return trueendmeta.__lt = function(a, b) return a<=b and not (b<=a)endmeta.__eq = function(a, b) return a<=b and b<=aendsetmetatable(a, meta)setmetatable(b, meta)print(a<=b)print(a<b)print(a>=a)print(b>a)print(b<a)
Unlike the algorithm meta-methods, tables must have the same meta-Methods for comparison.
3. Metadata method defined by the database
The above meta-methods are the core of Lua and are defined by the Lua virtual machine. In addition, various libraries also use their own fields to define the meta-methods. For example, print always calls the tostring method of table,
a = { "a1", "a2" }meta.__tostring = function(a) local l = { } for _,k in pairs(a) do l[#l+1] = k; end return "{"..table.concat(l, ",").."}"endsetmetatable(a, meta)print(a)
4. Metadata metadata
The most common access meta methods are _ index and _ newindex. Generally, Nil is returned when an element that does not exist in a table is accessed. However, if the table has the _ index metadata method, Nil is not returned, but this metadata method is called. You can use _ index to conveniently implement inheritance,
mt = { }mt.__index = function(t, k) return base[k]endbase = { b1 = 1, b2 = 2, b3 = 3 }derive = { d = 4 }setmetatable(derive, mt)print(derive.b1)print(derive.d)
When a value is assigned to an index that does not exist in the table, the _ newindex meta method is called,
mt = { }mt.__newindex = function(t, k, v) base[k] = vendbase = { b1 = 1, b2 = 2, b3 = 3 }derive = { d1 = 4 }setmetatable(derive, mt)derive["d2"] = 5print(base.d2)
Iii. Metadata example
The following is an example using the meta method to generate an iterative incremental table,
T = { container = { } }T.mt = { __add = function(a, b) local c = T.new{} for k,v in pairs(T.new(a)) do c[k] = v end for k,v in pairs(T.new(b)) do c[k] = v end return c end, __sub = function(a, b) local c = T.new{} for k,v in pairs(T.new(a)) do c[k] = v end for k,v in pairs(T.new(b)) do c[k] = nil end return c end, __tostring = function(a) local l = { } for k in pairs(a) do l[#l+1] = k; end return "{"..table.concat(l, ",").."}" end}T.new = function(t) if (t == nil) then t = {} end if (getmetatable(t) == T.mt) then return t end local r = {} for _, b in ipairs(t) do r[tostring(b)] = true end setmetatable(r, T.mt) return rendT.print = function(t) for k, v in pairs(t.container) do print(k) print(v) end endlocal mt = { __newindex = function(t, k, v) t.container[k] = T.new(v) end, __index = function(t, k) return t.container[k] end,}setmetatable(T, mt)T["first"] = { "a1", "b1"}print("elements in table first")T.print(T)T["second"] = T["first"] + { "a2", "b2", "a3", "b3"}print("elements in table first and second")T.print(T)T["third"] = T["second"] - { "a3", "b3" }print("elements in table first, second and third")T.print(T)
Element table and Element Method in Lua Programming