• Subject: # operator again... (sorry)
• From: Cosmin Apreutesei <cosmin.apreutesei@...>
• Date: Thu, 16 Jul 2009 18:50:52 +0300

```Hi,

[the following it's just my current understanding of lua language --
apologies for any noise]

Arrays are said to hold nils[1] but they cannot be iterated over
orderly, nor can they hold trailing nils. You can only _infer_ how
many nils are in between two contiguous sequences.

This is just a bonus you get out of implementing arrays over maps, and
not even a full bonus, since there's no sentinel at the end to allow
you to infer the number of trailing nils, and you cannot iterate them
orderly.

The inability to use a table to store and later orderly iterate a
vararg (which is a true sparse array) proves this point. That being
said, I don't mind this, as long as the "map" concept stays
consistent.

That solves sparse arrays (by denying them).

Now for the concept of contiguous arrays: ipairs() gives a simple and
consistent definition of a contiguous array for _any table_. I'd like
the # operator to follow that by always returning the index that
ipairs() would return last (that is, the last index in the sequence
that starts at 1). That way we would have contiguous arrays
consistently defined by all built-in functions and operators.
Currently, there's a subtile difference between
for i=1,#t do print(t[i]) end, and
for i,v in ipairs(t) do print v end

This change modifies the meaning of the following idioms in the
context of sparse arrays:
t[#t+1] = v   -- would always append to the array defined by
ipairs(); now the holes are filled randomly.
t[#t] = nil -- would always pop from the array defined by ipairs()
first; not it consumes sequences randomly.
t[#t] -- would always give the last element in the array defined by
ipairs(); now it gives the last element in a randomly chosen sequence.

Anything that could conflict with such changes?

[1] http://lua-users.org/lists/lua-l/2006-03/msg00576.html

```