[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: new "empty" value/type in Lua?
- From: Thomas Jericke <tjericke@...>
- Date: Tue, 02 Jul 2013 09:47:32 +0000
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).
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
It has been said, such solutions are already possible. So maybe the only
thing left to do is to define the "standard" solution.