[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Metatables index
- From: Jonathan Goble <jcgoble3@...>
- Date: Sun, 8 Nov 2015 19:26:10 -0500
On Sun, Nov 8, 2015 at 6:12 PM, Andrew Starks <andrew.starks@trms.com> wrote:
> You need a metatable with the __index metamethod defined. If
> you set that field to a table, then Lua will look at that table as a
> back up, such that these two lines of Lua will do the same thing:
>
> ```
> x = setmetatable({}, {
> __index = table
> })
>
> x = setmetatable({}, {
> __index = function(self, i)
> return rawget(table, i)
> end
> })
> ```
>
> Notice that in the second example, i used rawget. Lua does rawget when
> you use the first method. This was a design choice and the reverse can
> be done by just replacing the return line with `return table[i]`.
Nope. From the 5.3 manual: [1]
"If it is a table, the final result is the result of indexing this
table with key. (This indexing is regular, not raw, and therefore can
trigger another metamethod.)"
[1] http://www.lua.org/manual/5.3/manual.html#2.4