lua-users home
lua-l archive

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


On 30/06/2011 1.03, Sam Roberts wrote:
On Wed, Jun 29, 2011 at 12:25 AM, Lorenzo Donati
<lorenzodonatibz@interfree.it>  wrote:
* 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."

...

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

Wouldn't you also want to list keys of zero and positive integer keys>  n+1?

Its just a note, it doesn't have to be an exhaustive description of
the complement of the set of positive integers from {1...n}.


The note is relevant, not just nitpicking, because there is an important use case: the standalone interpreter.

Negative integer keys are used to store the command line arguments before the script name. Moreover arg[0] is the script name, which is always there. So someone could be mistaken thinking that "arg" is not a sequence, which is not true.


...

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.

In the set of positive keys {1..n}, how could it be correct to think n
is not positive?

Well, the reference manual is supposed to be formally correct (At least to some degree). There is no mention that {} (empty table) is a sequence (it is the "empty sequence" with length 0), so one has to infer that the notation {1..n} could indeed also refer to this case, and in this case n==0.

So it is perfectly clear that that notation also comprises the case {1..0} (meaning the empty set).

But (and this is a bit of nitpicking, I already said in my original post) if one has done that inference, could rightfully think that also {1..-4} means the empty set, so n==-4, and so the length may be -4.

It is an unintuitive inference, but perfectly logical, given that the notation {1..n} is not defined anywhere.

Moreover, this doubt may be reinforced by the past history of Lua. In Lua 5.1 #t returned an apparently "random" integer whenever t had holes. The manual stated very clearly the possible return values, but it was a rather convoluted explanation for people with little advanced math background.


And saying n is non-negative isn't the same as saying n is positive. 0
is non-negative and non-positive.

Yes, and since #{} == 0 (and that's not an implementation detail, as far as I can tell), you should say that the length of a table may be 0. The current wording doesn't mention that.

An alternative to my initial proposal may be to add an explicit reference to the empty sequence:

"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 *either* equal to {1..n} for some integer n *or to the empty set*. In *the first* case, n is its length, *in the second the length is 0*."

Of course you may find a still better wording.


Cheers,
Sam