lua-users home
lua-l archive

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


On Thu, Dec 6, 2012 at 10:04 PM, Andrew Starks <andrew.starks@trms.com> wrote:
> myobj:tier() do..." and accomplish the same thing, more clearly. (For that
> reason, I like the idea of writing explicit iterators, rather than
> redefining ipairs/pairs, but I'm probably in the minority on that one)

I personally use this approach because often I just want to iterate
over all the objects in a 'collection' (doesn't necessarily have to be
a table!).  Penlight's List class has a iter() method for this
purpose.

Did play with making these lists callable to restore 'for obj in list'
but it's a very suboptimal solution - the list ends up carrying
iterator state. Easy to confuse, and confusing.  So an __iter
metamethod does feel like a useful thing to have, although as Andrew
says, the for-statement becomes significantly harder to understand.
We do like to keep magic to a minimum...

steve d.