lua-users home
lua-l archive

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


On Thu, Jun 10, 2021 at 3:50 PM Xmilia Hermit <xmilia.hermit@gmail.com> wrote:

> If keys are removed it could raise `invalid key to 'next'` too.

I think that happens if the keys are removed and added, so we have two
sub-cases in case 3, but, anyway, we seem to have in the general
random case that next() could return nil, a key-value pair, or raise
an error. The latter behavior is actually documented for its low-level
counterpart lua_next(): 'This function may raise an error if the given
key is neither nil nor present in the table', so it seems no harm
would be done by mentioning this in the documentation for next().

So the full description then is:

A call to next() may raise an error if the given key is neither nil
nor present in the table, otherwise it returns nil or an existing key
with its associated value. When called with nil, it returns nil only
when the table is empty. A call without the second argument is
equivalent to a call with nil. If no new keys are added to the table
between the calls to next(), repeated calls to next() on the key
returned from a previous call to next() will enumerate all the keys
with their associated values and eventually return nil.

The order of the enumeration is unspecified and may change when new
keys are added during the enumeration, skipping some keys or returning
them multiple times, potentially infinitely if the modification
continues, or may raise an error if the keys are both added and
deleted.

Cheers,
V.