lua-users home
lua-l archive

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


On Fri, Aug 19, 2016 at 2:32 PM, Peter Aronoff <telemachus@arpinum.org> wrote:

> My question to you was: *why* change the manual’s definition? That
> definition is also simple and easy to test. And, frankly, it’s a lot more
> intuitive to me than yours. (As far as any of this is intuitive, at
> least...)
>
> P

I'm not intentionally proposing anything. Your point about the manual
is made by this example:

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> x = {10, 20, nil, 30}
> #x, table.unpack(x)
4 10 20 nil 30
> for i, v in ipairs(x) do print(i, v) end
1 10
2 20

table.unpack's behavior leads to one answer but a traversal through
ipairs gives us an inconsistency. Furthermore...

> x[10] = 100
> #x, table.unpack(x)
4 10 20 nil 30

...isn't necessarily obvious, given the value returned for length,
after the table's construction.

Therefore, yes. Because of table.unpack/pack, length and possibly
other Lua library functions, my assertion that Lua works the way that
I say it does is wrong. I choose to accept the manual as "wrong", as
far as my use of the language is concerned.

I typically use unpack and pack to deal with argument lists and
multiple return values, which are not necessarily sequences. There are
"significant" nils in this data structure, which is why table.pack
includes an `n` key, so that the length of this non-sequence can be
defined and this value can be used later by `pack`.

Where Lua and my understanding of it part ways is with length. To my
way of thinking, length should always return the value as I've
described it: the count of non-nil values from 1..n. That it doesn't
is strange to me and I don't see the point of it, other than as an
optimization that is worth more than the confusion that it causes me.

In real life, I don't typically use tables in a way where I am hit by
this surprise and I've learned to pay extra attention to __len, if I
expect to being doing weird things.

I hope that I don't sound like I am advocating for anything except the
validity of my perception and understanding of how things work.


-- 
Andrew Starks
612 840 2939
andrew@starksfam.org