4.24. Lua meta-table

发布时间 : 2025-10-25 13:34:08 UTC      

Page Views: 9 views

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 specified table sets the meta-table (metatable) if it exists in the meta-table (metatable) \__metatable key value setmetatable will 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 \__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) 

The output result of the instance is:

value1 metatablevalue 

Instance resolution:

  • mytable table is assigned to {key1 = "value1"} .

  • mytable meta-table is set, and the meta-method is \__index .

  • In mytable look up in the table key1 if found, the element is returned, and if it is not found, continue.

  • In mytable look up in the table key2 if found, return metatablevalue if you can’t find it, go on.

  • Judge whether there is a meta-table. __index method, if __index method is a function, the function is called.

  • Meta method to see if the parameter of the “key2” key is passed ( mytable.key2 set), return “metatablevalue” if the “key2” parameter is passed, otherwise mytable corresponding key value.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。