lua-users home
lua-l archive

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


2016-09-14 8:32 GMT+02:00  <Tomas.Lavicka@tieto.com>:


> It is very confusing and operator # can be unusable for tables,
> because in many cases I need to check sequences and I cannot
> trust this operator. It should return or 1 (book definition) or if holes
> in sequence break the sequence, it should return 0. Otherwise
> I will be pushed for every sequence check to check all array fields
> manually in some kind of loop.
>
> Tomas Lavicka
> (please excuse my bad english - I am not english native - but
> I hope that you understand, what I mean)

The word "should" in English can indicate uncertainty, as in
"Trying out something without reading the manual carefully
should normally give the result you expect." It can also describe
what happens in an ideal world postulated by the speaker, as
in "A program written by someone else should do what I want,
not what the author wants".

In the case of the length operator, you are not the first person
or even the hundredth who did not understand what it does
and how to use it properly. We all did. It is in fact the pons asinorum
that tests whether you are ready to travel on to the really subtle
things in Lua. About 10% of us also complained on Lua-L and
most of those also had the attitude that Lua or its documentation
is wrong. Only a select few (I was one) earn from Roberto the
response "Do not use the word 'bug' for something that you do
not understand properly."

Here is some background.

1. There is only one way to find the length of an array in O(1)
time, and that is to store it somewhere and update it when it
changes. In general one needs to know something about the
array in order to be able to do that. For this purpose, Lua offers
the __len metamethod. It says: "Dear Lua, I shall take personal
responsibility for deciding what the size of this array is.
Signed, A. User."

2. There is a way to find the length of an array in O(log n)
time if it is known that the array has the sequence property.
If you use the length operator without a metamethod, you
are saying: "Dear Lua, I know that my array is a sequence.
Please go ahead and find its length using your fast algorithm."

3. As Josh Billings put it, "The trouble with people is not what
they don't know, but what they know and it ain't so." If your
sequence ain't a sequence, #t will give you a frontier, i.e.
a filled position where the next one is empty. So you can
just keep on doing "t[#t+1]=newvalue" and never overwrite
anything already in the array. This behaviour used to be
documented, was removed because it might confuse some
people, but now that it has become clear that people get
confused anyway, we hope that the next Lua release might
restore it, since the implementation has not changed.

4. If you give Lua an array that is not a sequence, but
you thought is was, it is a bug, and the bug is not in Lua.
Preventing or even merely detecting that bug takes O(1)
would slow Lua up for everybody and is not a serious option.

I see a new message from you has just come in. Maybe
you have in the meantime discovered some things for
yourself that I have explained above. I am hitting Send
nevertheless since you will also not be the last person to
write about this.