lua-users home
lua-l archive

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


On 07/01/2013 08:44 PM, Roberto Ierusalimschy wrote:
Much more serious, for me, is the incompatibility that t[#t] = nil will
not "pop" elements form a list (and. of course, the extra complexity).

-- Roberto


That's why I suggested to have a new standard library "array". It won't be a new native Lua type. It's more or less just a standard metatable for array type tables. The only difference between an array and a sequence is, the array stores its length 'n' explicitly and therefore allows to have nils in it. Also the length is always set explicitly at construction or by calling setlength(n).

The metatable defines __len and __ipairs: The # oparator will always return n. The __ipairs always iterates from 1 to n, and doesn't care if there are nils in its way.

This is the minimal functionality needed. One could think of more options: You can override the __newindex to only accept keys between 1 and n. If setlength(new_n) is called, all elements after new_n become nil, if new_n < n. But these would just be extra features to make life easier.

It has been said, such solutions are already possible. So maybe the only thing left to do is to define the "standard" solution.

--
Thomas