lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:
> 
> > is possible, i would especially like
> >
> > for a,b in MyObject:Get do
> 
> You can write
> 
>   for a,b in MyObject:Get() do ... end

But that wouldn't be the same.  You call Get once, the former would
call Get repeatedly (passing MyObject as the first arg each time).
That way you can avoid the creation of a closure. [1]

Ciao, ET.

[1] But then one has to manage the for-state within MyObject which
would prohibit multiple for-loops over the same object at the same time.
I.e. "for a in x:f do  for b in x:f do ... end  end" would break.
For-loops normally manage iteration state for you.  That's not the
case with the for-in-function loop.  It has been degenerated to a
simple "while a=f() do...end".