lua-users home
lua-l archive

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




On Monday, June 1, 2015, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> The right question is not whether a table is a sequence, but whether
> the default #tbl is deterministic for the particular table, i.e. whether the
> table has no holes. Being a sequence is merely a sufficient condition. The
> iterations `for i=1,#tbl` and `for i in ipairs(tbl)` both ignore
> non-integer keys.

Sorry, but if you want to know whether '#tbl' has a reasonable meaning
(and is deterministic), then the right question is whether a table is
a sequence, because that is what the manual 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
  numerical keys is equal to {1..n} for some non-negative integer n. In
  that case, n is its length.

The fact that the current internal implementation of '#' ignores
non-integer keys is an implementation detail.

-- Roberto


Types! I asked this question last year and was told that I could depend on this behavior, but not by you or anyone else that is official. Nobody corrected the answer either. 

I depend upon this "implementation detail" quite often. I use it as a "sequence with properties."

I imagine that enough code would break that any implementation that did not respect this would be considered broken by many developers / libraries. 

I'll use that as justification to continue depending on it, although the knowledge that this is not a part of the definition is unsettling. 

Also, I thought that "#" was changed to stop at the first nil in 5.3.

-Andrew