lua-users home
lua-l archive

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


在 2015/6/6 12:33, Tim Hill 写道:

My view of this is simple; a sequence is the Lua equivalent of an array (or as near as it gets to one). Other languages have survived for decades only allowing positive integral indices in arrays, why is it a problem in Lua?

If you have non-integral numeric keys, you don’t have an array; you have a dictionary with arbitrary keys that happen to be numeric. The # operator is pretty meaningless in this case.

If you really have need for a set of data items indexed by integer keys (an array), and an additional set of *different* data items with non-integral keys, imho you are better off keeping these in discrete tables (perhaps inside a top-level table); segregating data by clever tricks with the data domains is always messy in my experience.

—Tim


I was sometimes thinking maybe we've made it too explicit that
a table in Lua had an __array part__.

the table type in Lua serves two distinguashed purposes:
 1) as a compound data type, like struct or record in other languages
 2) as a container data type, like array or list in other languages

many are told that a table has an array part and a hash part. thus many
tend to mix compound data and container when they are thinking in Lua.


-- the nerdy Peng / 书呆彭 / Sent from Thunderbird