lua-users home
lua-l archive

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


On Wed, Sep 17, 2014 at 7:16 AM, Steven Degutis <sbdegutis@gmail.com> wrote:
> Quoth polyglot:
>> 5.2 REFERENCE MANUAL
>>
>> "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."
>>
>> *** Conclusion ***: Without a __len metamethod, an empty table has undefined
>> length.
>
> Not so. An empty table is a still considered a sequence, just with no
> elements. The manual could be a little more clear on this, but it's
> not saying the table must contain elements to be considered
> sequential. It is only differentiating between types of non-empty
> tables.

More precisely:

What is the set of positive numeric keys for an empty table? Does the
set of positive numeric keys for an empty table obey this definition?

This set of keys is {}, the empty set. It IS a member of the set of
sets of the form {1..n}, with n = 0. (Try it with a for loop -- for n
= 1, 0 do print(n) end -- and see that this is true.)

Therefore, an empty table is a sequence, and it is a sequence of length 0.

And because an empty table is a sequence with length 0, the length
operator is defined on it.

/s/ Adam