lua-users home
lua-l archive

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


On Thu, Oct 10, 2019 at 4:21 PM Roberto Ierusalimschy wrote:
> What api would you prefer for next? And how did its current api spoil generic for? Thanks.

Probably 'next' should return first some opaque "index" object, to be used
in its next call, and then the key and the value. (The index object could
be, for instance, an integer with the internal current position of the
traversal, but that should not be in the specification.)

Similarly, the control variable in the 'for' loop should be hidden like
the state, and not the first variable declared in the loop.


index_obj, key, value = next(t, prev_index_obj)
Which problems such index object could solve?

1) As for now, we could not use nil as a table key.
Obviously, new version of next() could make t[nil] valid because next() will correctly handle a nil key.
But do we really need this?

2) As for now, we are not allowed to create new table keys while traversing the table with pairs()
Could introducing of "index object" solve this problem?
We do really need this.