lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 27-Feb-07, at 12:22 PM, Mark Hamburg wrote:

I agree that pairs should be changed to look for a __pairs metamethod. Doing
so would resolve a number of problems with using proxy tables.
(Interestingly, ipairs needs no such help...)

Yes, although in some cases one might want to assign ipairs as
the __pairs metamethod of a table :)

The code in the GeneralizedPairsAndIpairs module generalizes
pairs() and ipairs() in different ways; the pairs() implementation
looks for a __pairs metamethod, while the ipairs() implementation
simply uses lua_gettable() rather than lua_rawgeti(), allowing
the use of ipairs() with, for example, userdata which are indexed
with integer keys.

In practice, the generalization of ipairs() proved to be less
useful than I thought it would. In the vast majority of cases
where I would want ipairs() to honour __index metamethods, I
would also want it to be the implementation of __pairs().

What would, I think, be useful would be allowing the #
operator to respect a __len metamethod for a table, and
not just for non-table datatypes.