lua-users home
lua-l archive

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

>>From Lua 4.0 onwards, every version of Lua has had:

* a notion of table length
* some functions that operate on tables as lists, arrays, sequences,
call them what you will, including 'ipairs'

and those have changed every time. Yet another change is in the
offing, if not in 5.4, then surely in 6.0.

The problems that Roberto are trying to solve [1] are these:

> 1) A constructor like {x, y, z} should always create a sequence with
> three elements. A constructor like {...} should always get all arguments
> passed to a function. A constructor like {f(x)} should always get
> all results returned by the function. '#' should always work on these
> tables correctly.
> 2) A statement like 't[#t + 1] = x' should always add one more element
> at the end of a sequence.
> These are what confuse people all the time, these are what start new
> rounds of discussions around '#'.
> Once 'empty' and 'undef' are values, you break all those properties.
> I think we cannot use a value to represent the absence of a value, no
> matter how many no-value values we add. It is a contradiction. Several
> languages followed this path, always with bad results.

Ironically, the first of these problems [2] would be much easier to solve
if the current version were Lua 5.0 than 5.3. From Lua 5.1 onwards,
the dreaded border function was introduced, and the "endless rounds
of discussion" ensued.

Lua 5.0 had a function table.setn, described in the manual as follows:

> Updates the size of a table. If the table has a field "n" with
> a numerical value, that value is changed to the given n.
> Otherwise, it updates an internal state so that subsequent
> calls to table.getn(table) return n.

That internal state was set to the size of the table as initially created,
and automatically updated whenever one used table.insert  or
table.remove. It was not updated by assigning a value to tbl[n+1]
or by assigning nil to tbl[n].

Unfortunately, the initial value was not taken from the length of the
table constructor. Nils at the end of the table were elided.

But that small change, of setting the internal state to the actual size
of the constructor, would be all that is necessary to solve all three
parts of the first of the stated problems, without breaking either
of the two characteristic properties of Lua which have persisted from
Lua 1.0 right through to Lua 5.4 work2:

* "Semantically, there is no difference between a field not present
in a table or a field with value nil."

* "Tables are the sole data structuring mechanism in Lua".

[1] Quoted from Roberto's post

[2] For which purpose table.insert(t,x) and table.remove(t) are
specifically designed.