Lua Lookup Table element procedure (meta-table, how the __index method works)

Source: Internet
Author: User
Tags lua

The table essence of Lua is actually something like HashMap, whose elements are many key-value pairs, and if you try to access an element that does not exist in a table, itThe set of lookup mechanisms that trigger Lua is also the mechanism by which "object-oriented" can be achieved.


To illustrate:
[Plain]View Plain copy print? 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:
In my understanding, the meta-table is like an alternate lookup table, and frankly assuming that table A's meta-table is B, then if you can't find something in a, you will try to find it in B.


__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:


give a chestnut:)
[Plain]View Plain copy print? Father = {House=1} son = {car=1} setmetatable (son, Father) – set son's metatable to Father print (Son.ho Use) output is nil, but if you change the code to
[Plain]View Plain copy print? Father = {house=1} Father.__index = father– points Father __index method to itself son = {car=1} setmetatable (so N, 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


Original link: http://blog.csdn.net/xocoder/article/details/9028347

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.