[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Does PIL (3rd edition) repeatedly misuse the length operator on tables and invoke undefined behavior?
- From: Steven Degutis <sbdegutis@...>
- Date: Thu, 18 Sep 2014 11:33:43 -0500
Quoth Tim Hill:
> No, I think the OP is correct insofar as what the reference manual says. My
> reading of the strict definition of sequence is that an empty table is NOT a
> sequence, and therefore the result of # is indeed undefined, regardless of
> what it actually returns in practice.
Given the manual wording in question:
"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. In that case, n is
its length."
It says n may be any integer, and 0 is a valid integer.
The only two arguments against this are:
1. n must be >= 1 due to the use of {1..n} in the definition, and
2. that n may be a negative number (e.g. -14), thus making length = -14
My arguments:
1. The definition does not say that n must be >= 1, only that it must
be an integer; so we rely on whether 0 is a valid length, which we
hold as self-evident.
2. The concept of length precludes the possibility of a negative
length, excluding all negative values of n.
Thus no clarification in the manual is required, so long as these
principles are considered. Therefore I believe the current wording of
this manual quotation is satisfactory.
-Steven