lua-users home
lua-l archive

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


On Sat, Jul 28, 2007 at 04:38:39PM -0400, John Belmonte wrote:
>     #t                 -- non-deterministic
>     unpack(t)          --       ""
>     ipairs(t)          --       ""

I think the main problem is in this non-deterministic way to handle
arrays.

A simple way to handle this can be to slightly modify the definition of
the array part of a lua table :
#t is the biggest 'n' such that all values t[1], ..., t[n] are not nil.
The array part of a table 't' is a sub-table t[1], ..., t[#t].

In this case we have a perfectly deterministic array without holes. The
cost to do this is negligible. (The implementation can continue to store
other keys after a hole in the array part of the table structure for
efficiency but these are not exposed to lua as part of the array)

If people want hole in the array they can choose to use a special value
for it as suggested in previous messages. But an array is a set of
non-nil values indexed by contiguous integer starting from 1.

This is probably not the solutions wanted by most of the users, but it
simple, efficient (and elegant in my point of view) and easily allow
users to emulate other definitions.

tom

-- 
Thomas Lavergne                       "Le vrai rêveur est celui qui rêve
                                       de l'impossible."  (Elsa Triolet)
thomas.lavergne@reveurs.org                           http://reveurs.org