[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A Type Metatable for Tables?
- From: "Greg Falcon" <veloso@...>
- Date: Wed, 20 Feb 2008 14:03:49 -0500
On 2/20/08, John Hind <john.hind@zen.co.uk> wrote:
> Reading Roberto's "Programming in Lua" 2nd Edition, I was struck by the
> inconsistency between the String library and the Table library - in Lua 5.1
> the former's functions can be used as methods on a String object while the
> latter can only be used as "traditional" library functions.
>
> Of course the reason is that Strings have one per-type metatable while Table
> metatables are per-instance. There is currently no way for the Table library
> to set a metatable for all Tables.
No, the reason you can't do this is that my_table:sort() is syntactic
sugar for my_table['sort'](my_table).
If my_table doesn't have 'sort' as a key, then my_table['sort'] sure
had better return nil, or else Lua no longer has a reliable
associative container type.
Greg F