lua-users home
lua-l archive

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


RLake:
> Of course, that is true. However, that assumes that you always know the
> iteration type of the table you are trying to iterate. A general pairs
> function which consults the metatable to extra the __pairs metamethod is
> not significantly slower than the existing pairs function, and avoids
> iterators having to intimately know their subjects.

Bilyk, Alex:
> You can implement this functionality in Lua itself by re-impelemting
> 'pairs' function from standard library to *your* linking. Otherwise, what
> if after you are done with your __pairs idea someone else pops up and says
> that it is still not enough for whatever reason and suggests to have
> another metaevent "__pairs_2" to accomodate for that whatever? Where do
> you draw the line?

You draw the line at standard inbuilt functions (of which there are only a
small finite number). Metamethods are designed to get at the internals of
Lua functions in an abstract way, while user defined functions are the
programmer's own problem.

With interation there seems to be multiple inbuilt angles of attack
(table.foreach, pairs, next, deprecated 'for' on table) with no strong
documentation as to where exactly each of these gets their 'next' function.

Lua is currently half way to making user simulated table behaviour (with
__index & __newindex). How about going the rest of the way with a __next
metamethod?

Note: I'd use "__next" rather than "__pairs" because it is more fundamental.

*cheers*
Peter Hill.