Lua lookup Table element process (meta-table, how the __index method works) (reproduced)

Source: Internet
Author: User

The article derives from the Lua lookup table element process (meta-table, how the __index method works)

The table essence of Lua is actually something like HashMap, and its elements are a lot of key-value pairs, and if you try to access an element that doesn't exist in a table, it triggers a set of LUA lookup mechanisms that can be "object-oriented" by virtue of this mechanism.


To illustrate:

temptable = {}  print (Temptable.membera)--This is an attempt to print a member that does not exist temptable Membera  

Execution Result: nil
The reason for the output to nil is very simple, temptable does not membera this member, which is in line with our usual knowledge of hashmap. But for LUA tables, if TempTable has a meta-table, the situation is different.


What is a meta-table:
MetaTable is an "Operation Guide" that contains a series of operational solutions, such as the __index method, which defines what the table should do if the index fails.


__index Meta-method:
According to the previous argument, if A's meta-table is B, then if a member that does not exist in a is accessed, it will be accessed if there is no such member in Find B. This is generally the case, but it is not quite so, in fact, even if A's meta-table is set to B, and B does have this member, the return result will still be nil, because the __index meta-method of B is not assigned a value. As I understand it, the__index method is used to determine how a table looks when it is used as a meta-table . That's a bit of a detour. So:


As an example:

Father = {  house=1  }  son = {  car=1  }  setmetatable (son, Father)--set son's metatable to father  Print (Son.house)  

The result of the output is nil, but if you change the code to

Father = {      house=1  }  father.__index = father--point father's __index method to his  son = {      car=1  }  setmetatable (son, Father)  print (Son.house)  

The result of the output is 1, which matches the expected


This, in conjunction with the above example, explains the meaning of the __index meta-method:

In the above example, When visiting Son.house, there was no House member in son, but Lua then found that son had a meta-table father, and then father was looked up as a meta-table, at which point Lua was not directly looking for a member named House in father, but instead called Father's __index method, if the __index method is nil, then nil is returned, if it is a table (the __index method in the example above is equal to itself, this is the case), then to the __index method refers to the table to find a member named House, so, Finally found the House member.
Note: The __index method can be a table in addition to a function, and if it is a function, the return value of the function is returned when the __index method is called.


Here, a summary of the rules that LUA finds for a TABLE element is actually the following 3 steps:


1. Find in the table, if found, return the element, cannot find then continue

2. Determine if the table has a meta-table, if there is no meta-table, return nil, a meta-table will continue

3. Determine if the meta-table has a __index method, if the __index method is nil, then return nil; If the __index method is a table, repeat 1, 2, 3, or return value of the function if the __index method is a function

Lua lookup Table element process (meta-table, how the __index method works) (reproduced)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.