lua-users home
lua-l archive

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


On Wed, 19 May 2010, Gavin Wraith wrote:
>
> Postscript. In view of the troubles with holes, one might prefer
> to deal with arrays of non-nil values. So a table x is an Array (to
> distinguish from any previous notion) if either it is {} or
> there is a positive integer n such that x[i] is non-nil for
> i = 1, ... ,n and is nil for all other keys. For such a table I
> presume that #x = n. Then the Array-part of a table t is an Array x
> for which (t[i] and x[i] and t[i] == x[i]) is true for all i = 1, ... ,n.

Your definition requires an O(n) search to evaluate #x, whereas Lua's
definition allows an O(log n) search.

> Seeing that nil is useful for denoting error conditions, and that it is
> useful to distinguish errors, has any consideration been given to having
> lots of different, possibly user-definable, nils? One might want a table
> t to have its own nil, say nil_t, which would be the value returned
> by t[k] if the key k is not defined for t.

nil_t = {}
setmetatable(t,{ __index = function () return nil_t end })

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/  【ツ】
ROCKALL MALIN HEBRIDES BAILEY: SOUTH OR SOUTHEAST 4 OR 5, OCCASIONALLY 6 IN
ROCKALL AND BAILEY. MODERATE, OCCASIONALLY ROUGH IN ROCKALL AND BAILEY.
OCCASIONAL RAIN OR DRIZZLE, FOG PATCHES. MODERATE OR GOOD, OCCASIONALLY VERY
POOR.