• Subject: Lua 5.2 sequence definition - clarification needed
• From: Lorenzo Donati <lorenzodonatibz@...>
• Date: Wed, 29 Jun 2011 09:25:23 +0200

Hi all!

* 1st point

Section 3.4.6 of Lua 5.2 refman says:

"Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is, the set of its positive numeric keys is equal to {1..n} for some integer n."

and later:

"Note, however, that non-numeric keys do not interfere with whether a table is a sequence."

From the first sentence I get that also non-positive numeric keys don't interfere with the table being a sequence (right?). Wouldn't it be worth adding this note to the second sentence? I.e.:

"Note, however, that non-numeric keys *or non-positive numeric keys* do not interfere with whether a table is a sequence."

* 2nd point

This is maybe nitpicking, but anyway:

as someone else already pointed out in an old thread (IIRC), the first cited sentence above assumes that an empty sequence is a table that has no positive numeric keys. In this case the above definition would hold with the set being {1..0}, i.e. the empty set. But the empty set could also be represented as {1..-1}, or {1..-4} with that notation. Therefore someone could infer that for empty sequences the length could be either 0, -1 or -4.

Therefore I would modify slightly the definition:

"Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is, the set of its positive numeric keys is equal to {1..n} for some *non-negative* integer n."

This would rule out that (admittedly unintuitive, but correct) misinterpretation.

Cheers.
-- Lorenzo