lua-users home
lua-l archive

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



On 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.

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.

(And yes, I’m on record as not liking this definition.)

As I have noted before, since Lua provides no means to determine if a table is a sequence, it follows that unless you have control over the creation of a table, the length of the table is undefined, since your code cannot know if it is a sequence. This makes writing robust APIs that consume 3rd party tables problematic.

—Tim