[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How about removing the underscores from metatables?
- From: Michal Kottman <k0mpjut0r@...>
- Date: Thu, 27 Jan 2011 23:48:25 +0100
On 27 January 2011 21:36, Steve Litt <slitt@troubleshooters.com> wrote:
> On Thursday 27 January 2011 15:18:28 Javier Guerra Giraldez wrote:
>> On Thu, Jan 27, 2011 at 3:09 PM, Steve Litt <slitt@troubleshooters.com>
> wrote:
>> > Steve, you just lost me. I thought __index is the function that gets done
>> > when you write-access a key in the table. But A is the whole table, not a
>> > function. What am I missing?
>>
>> __index can be a function or a table.
>>
>> a common idiom is to use the same metatable as __index; so you put
>> there both the metamethods (__xxx) and OO-style methods
>
> I'll have to do more reading on metatables, and keep in mind that__index can
> be a function OR a table.
Reading Programming in Lua by Roberto Ierusalimschy
(http://www.lua.org/pil/) is VERY recommended. It is very
friendly/easy reading, and explains all main ideas and idioms in Lua.
For example, the chapter 13.4.1 (The __index Metamethod,
http://www.lua.org/pil/13.4.1.html) states the following:
The use of the __index metamethod for inheritance is so common that
Lua provides a shortcut. Despite the name, the __index metamethod does
not need to be a function: It can be a table, instead. When it is a
function, Lua calls it with the table and the absent key as its
arguments. When it is a table, Lua redoes the access in that table.