[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Empty? No. Array? No. Has? Yes.
- From: Sean Conner <sean@...>
- Date: Sat, 6 Jul 2013 20:31:38 -0400
It was thus said that the Great Jay Carlson once stated:
> Let's take the following code, applied to an array-like table:
> i = #t
> t[i] = nil
> This could mean three things:
> 1) Shorten t by 1 by deleting the last element of the sequence. It
> becomes shorter.
> 2) Make future references to t[i] return nil; the length is not
> important. (It's just used like a table.)
> 3) Make the sequence member t[i] return nil; that is, store nil but take
> no action on length.
> Note that metatables can help for a fixed length array; __len can always
> return (say) 256, always producing behavior 3. But there can't be a
> shrinkable array.
> So, for total overkill, I introduce new syntax to make intent clear:
> i = #t
> delete t[i]
That already exists in Lua: table.remove(). It even allows one to remove
an arbitrary entry anywhere in the array.