[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: new "empty" value/type in Lua?
- From: Scott Fial <scott@...>
- Date: Tue, 02 Jul 2013 14:05:55 -0700
Just another array idea to clutter the discussion...
Provide a syntax to specify an explicit array-length for a table t, e.g.:
#t = 100
Once an explicit array-length has been set for t, let it take on the
following new behaviors:
1. type(t) shall return "array"
2. #t shall return the array-length rather than lua's internally
computed length (although the internal length is still
maintained)
3. For any integer index i > array-length, the assignment t[i] = v
shall have the side-effect of setting array-length to i (even
when v is nil).
4. ipairs(t) and unpack(t) shall iterate over the entire range 1..#t
5. Setting the array-length to nil shall remove the explicit
length and restore the table to normal behavior.
EXAMPLE SESSION
Supposing that the parser is modified to allow the # operator in an
l-value context as a syntax for setting an explicit length for a table
(I have no idea if this is feasible), here's an example interactive session:
> t = {1, 2, 3}
> print(type(t), #t)
table 3
> #t = 5 -- set explicit array-length
> print(type(t), #t)
array 5
> for i, v in ipairs(t) do print(i, v) end
1 1
2 2
3 3
4 nil
5 nil
> #t = 2 -- array-length explicitly decreased
> print(unpack(t))
1 2
> t[6] = 6 -- array-length implicitly increased
> t[7] = nil -- and again
> print(unpack(t))
1 2 3 nil nil 6 nil
> #t = nil -- restore normal behavior
> print(type(t), #t)
table 3
Scott