lua-users home
lua-l archive

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


The recurring discussion of the precise delineation of the undefined region
for the 'length' of a table obscures the possibility that the root problem
is not the knowing of the unknown but rather the non-obvious nature of the
unknown. If something is hard to explain, it probably needs implementing
better, not explaining better!

If Lua had an 'oldindex' metamethod, it would be possible to eliminate the
unknown. 'oldindex', if present, would be fired on any attempt to assign a
nil value to a key with a non-nil value. It would receive the table, the key
and the old value and return the new value (which could be nil to proceed
with the 'deletion' or the old value to block it).

Combining 'oldindex' with existing features ('len', 'newindex' and tracking
variables) we could:

- Constrain a table to be a proper vector/list (so length would always be
valid).
- Redefine length so it is a defined value for the previously undefined
cases (0, -1 or nil for example).
- Redefine length so it is a true count of all the elements in the table
regardless of key.
- Redefine length so it is the largest positive integer key regardless of
'holes'.

[OP braces self for death by a thousand nit-picks and random digressions!]

Attachment: smime.p7s
Description: S/MIME cryptographic signature