[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: '__iter', yet again!
- From: Tobias Kieslich <tobias@...>
- Date: Thu, 17 Dec 2009 12:04:09 -0800
> The beauty of the language, is that it has very few concepts, used in
> many places. Rather than having a specific mechanism to iterate over
> objects, the for loop use the mechanisms of multiple return values and
> first class functions. The resistance comes from the fact that you
> want to make Lua more complicated, just to save an explicit method
> call. And that's assuming that the implicit __iter is better syntax
> than an explicit method, which is arguable (e.g. I would disagree with
> you). And concise and cryptic syntax overriding isn't even part of OO,
> it's just often bundled with it.
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 what
can be done with __iter?