lua-users home
lua-l archive

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


On Thu, 11 Jun 2015 11:12:07 +0200
Oliver Kroth <...> wrote:
> Mike,
> 
> the behaviour is unpredictable because it is defined to be not defined:
> 
> > " 3.4.7 – The Length Operator
> > ...
> > Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is, the set of its positive numeric keys is equal to {1..n} for some non-negative integer n. In that case, n is its length. Note that a table like
> >
> >       {10, 20, nil, 40}
> >
> > is not a sequence, because it has the key 4 but does not have the key 3. (So, there is no n such that the set {1..n} is equal to the set of positive numeric keys of that table.) Note, however, that non-numeric keys do not interfere with whether a table is a sequence. "
> >

I see, it is reasoned. If we strictly follow the logic,
the length operator should ideally return nil in theese cases,
because "it usually represents the absence of a useful value".

--