lua-users home
lua-l archive

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


> On 25 Jun 2016, at 03:22, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 
> Well, actually all that 'pairs(tbl)' does is to return 'next,tbl,nil', but
> it does so in C, which is ever so much nicer. So all you need is:
> 
>   for k,v in mynext,tbl do ...
> 

I may have explained poorly but I need the reverse.  I have a different looping structure in which I also need to iterate a userdata structure and so I use `next()` to step to the next key/value when it is needed within the context of the outer loop.  This would be the sort of place where you would use a producer/consumer coroutine model (but coroutines are overkill in this instance).  All I wanted was to have a common metamethod that applied to both `next()` and `pairs()`.

I was thinking of `next()` as the common factor but as Soni pointed out the reverse is actually true, `pairs()` returns the required tools and `next()` just happens to be the built in default.