[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: '__iter', yet again!
- From: "John Hind" <john.hind@...>
- Date: Fri, 18 Dec 2009 11:51:44 -0000
Like most metamethods, "_iter" is not intended to be defined in
out-of-the-box Lua (anymore than you would expect to have an "__add"
metamethod on a baseline table or string). It is intended (by me anyway) to
support OO programming by allowing the class designer to specify a default
iterator factory for each class of objects. This feature would not change
Lua in any way unless you chose to use it. (Well to be precise, it *would*
disable the version of the "__call" trick that avoids the empty parenthesis,
but it replaces that undocumented trick with a documented and better way of
achieving the same thing.)
Also the function you would assign to "__iter" would have exactly the same
signature as 'pairs' or 'ipairs' (in fact you could use these functions if
they were appropriate). So you can specify the iterator factory to return
the key or to store the key in a closure and hide it, whichever is
appropriate to that class.
I say again, I say again, I say again: "_iter" is just another metamethod.
It does not take anything away or alter the basic use of Lua in any
(significant) way. All it does is provide fallback behaviour for a situation
which would otherwise generate a runtime error - and it is a fallback that
is not there unless you put it there!
One more time to be sure: "__iter" is just another metamethod!
> -----Original Message-----
> From: email@example.com [mailto:lua-
> firstname.lastname@example.org] On Behalf Of Tobias Kieslich
> Sent: 17 December 2009 20:04
> To: Lua list
> Subject: Re: '__iter', yet again!
> just a question, being also a Python programmer, I see why
> for x in obj do
> can be handy, on the other hand, when in Lua 5.1 you iterate over a
> strictly array based table, you would use ipairs and toss the numerator
> if you don't need it. So, in 5.2 it seems we get __pairs and __ipairs
> which would make a construct like:
> for _,x in ipairs(obj) do
> possible. That is clear Lua syntax, very explicit and more importantly
> more flexible than __iter since it does allow you to have numerators if
> you need them. I think __iter would implement a functionality that is
> only half of what it could be, introduces a new concept (__iter would
> have to be implemented for strings and tables as well to be consistent)
> and while it would be a convienience shortcut, it also can cause
> See, python has the for x in obj, for x in iter(obj) and
> for x in enumerate(obj) and that get's confusing after a while. I very
> much prefer Lua's explicite syntax here.
> For the question is for _,x in ipairs(obj) do that musch worse than
> can be done with __iter?