lua-users home
lua-l archive

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


On 6/11/2015 7:14 AM, Dirk Laurie wrote:
2015-06-11 10:29 GMT+02:00 Mike <lua-l@inbox.ru>:
I cannot guess the exact rule for the length operator
....
In the Lua 5.1 manual, the following boundary property was
guaranteed:

     t[#t] is not nil, but t[#t+1] is.

The exact quote from the 5.1 manual is "The length of a table t is defined to be any integer index n such that t[n] not nil and t[n+1] is nil; moreover, if t[1] is nil, n may be zero."

This allows #{} to be zero as it obviously is, and for t[#t+1] to extend any sequence, even an empty sequence.

For some cases, the guarantee that t[#t+1] is nil is all that is needed; it provides a guaranteed empty slot in a table used like the registry, even if the keys do not form a sequence.

That property seems useful, and I am still slightly puzzled about why it was dropped from later versions. At least it remains clear that non-numeric keys have no effect on the sequence-nature of a table, so it is acceptable to have sequences with attributes.

This is no longer documented, perhaps because the user can now
supply a __len that does not have that property.

Obviously a user supplied __len should completely replace the entire definition of `#` for tables with that __len.

--
Ross Berteig                               Ross@CheshireEng.com
Cheshire Engineering Corp.           http://www.CheshireEng.com/