[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**Subject**: **Re: __iter metamethod?**
**From**: "steve donovan" <steve.j.donovan@...>
**Date**: Tue, 9 Oct 2007 10:32:28 +0200

Thanks, Ben - that's exactly what's needed. Nice to see that it can be
done with an extension.
steve d.
On 10/9/07, Ben <thebassplayer@gmail.com> wrote:
> Have you looked at http://lua-users.org/wiki/GeneralizedPairsAndIpairs ?
>
> On 10/9/07, steve donovan <steve.j.donovan@gmail.com> wrote:
> > Hi guys,
> >
> > Has anybody given some thought to the usefulness of an __iter
> > metamethod? It would specifically be called by a for-statement if it
> > was not given an explicit iterator function. For example,
> > t = {10,20,30}
> > setmetatable(t,{
> > __iter = ipairs
> > }
> > for i,v in t do
> > print(i,v)
> > end
> >
> > Which is precisely the old Lua 5.0 behaviour ;)
> >
> > But this can be generalized; file objects could have an __iter
> > metamethod, and then one can iterate over all input lines in a
> > Pythonesque way:
> >
> > for line in io.stdin do
> > ...
> > end
> >
> > The motivation is to make the concept of _sequence_ more useful in Lua...
> >
> > steve d.
> >
>