lua-users home
lua-l archive

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


> 2) The length operator returns the last index before a nil in an array

Just before someone reads it wrong again. No it doesn't.
And I think the notion is correct, _we_ know what # means, but Lua at
least in its root has not been targeted at the expert that reads the
manual very closely, but aimed to be a simple as hazard free language
as possible. For _us_ # is perfect, for the Lua-newbie it can easily
become a hazard.

My suggestion is: remove any default core implementation of # on
tables. # just calls __len on the metatable and thats it, nothing
else.

Then provide in the core library a "List" function [1] returning a List table.
a = List{a, b, c}.

This one has the metatable __len set to define #. Any other table hasn't.
List can either maintain the length itself, or rely on some #-like
core functionality provided by any other means to the userspace. A
list can throw an error if any operation would make it a non-list or
if you want to do it else lose its list ability and thus its metatable
(throwing an error when # is later called upon). table.insert -- or
list.insert ? -- can also rely on the #-like core functionallity thats
not called # anymore.

This way I hardly see perfomance loss.

[1] I'd call it "List" since this is the more generic term for a
linear organisation. Array has a specific meaning in informatics, and
the list implementation not necessary has to be an arrray. Also to
hinder confusion with the cores "array" and "hash" part.