lua-users home
lua-l archive

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


Alex Bilyk escribió:

>>
You can do this with generalized 'for' construct in Lua 5. All you have to
do is to write a function that generates 'next' pair the way *you* want it.
For instance, one could write a generator function that iterates through
every sub-entry within a given table hierarchy or only some designated
entries satisfying some criteria. In your case, the criteria would be to
pay attention to "__index" and include those into your iteration domain. No
new metamethods are needed for this.
<<

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.

R.