lua-users home
lua-l archive

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




On 24/06/16 09:14 AM, Duane Leslie wrote:
On 24 Jun 2016, at 19:20, Soni L. <fakedme@gmail.com> wrote:

next() is supposed to be raw. You should be using `local f, o, i = pairs(obj)` and passing f, o and i around.
Huh, I didn't think of that, but obviously that solves my problem.  I can just patch my code with the following:

```lua
function next (t, k)
     return pairs(t)(t, k)
end
```

I retract my request.  Excuse the noise.
A few issues with that:

1. If pairs(t) returns a stateful closure, which ignores arguments, you'll always return the first result.
2. It breaks code that relies on next() being raw. Such as:
    - https://gist.github.com/SoniEx2/fc5d3614614e4e3fe131/
3. It's slower than even a monkeypatched (with debug.getmetatable) __next metamethod, as pairs() returns 3 values and you risk allocation for the unused values.

It would be better to rewrite your code to take in a proper iterator, so you do e.g. f(pairs(t)) instead of f(t).

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.