4.24. Lua meta-table

发布时间 :2023-10-12 23:00:02 UTC      

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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.