lua-users home
lua-l archive

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


2009/11/13 spir <denis.spir@free.fr>:
> I find great that lua introduced proper iterators, including generators, without addition of any new language feature such as continuations and magic "yield" statements. Actually, the magic is rather done by lua under the hood but does not directly affect the user --if I understand correctly.
> Still, after a first lucky trial, I had rather hard and long time guessing how to properly interpret the syntax (esp the "invisible" variables) and consequentely how to implement iterators for various uses -- esp. the highly different cases of collection and generator iterators, respectively. I was surprised to endly discover that defining the iterator func (the actual data yielding one) inside the iterator allows getting rid of the said invisible vars alltogether.
> I may be wrong, sure, maybe in some cases they are needed, still maybe we could simplify the syntax in a way that would let using this great feature much more easily. I wrote a doc about this, but put it on the wiki for it's rather long:
> http://lua-users.org/wiki/SimplerForIterator
>
> (Took a risk if I'm totally wrong ;-) & hope it's not full of stupidity, because there it will remain -- but anyway a wiki is intended to be corrected.)

Removing these "invisible" variables would force all iterator
functions to be specially instanciated closures with upvalues. This is
a performance hit. The following wouldn't be possible any longer :
    for k,v in next,t,nil do print(k, v) end

On the other hand the current syntax doesn't force you to use these
hidden variables. Just ignore them.