lua-users home
lua-l archive

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


Well this is an interesting and illuminating thread. But I think there's actually some confusion on what I was originally asking/suggesting.

On Sat, May 30, 2015 at 11:28 PM, Brigham Toskin <brighamtoskin@gmail.com> wrote:
Does anyone know the rationale for not just automatically tracking the number of entries in a table?

This was meant to be a question about tracking the physical number of non-nil entries in the internal table data structure. So for instance, something like:

> print(numentries{1, nil, 3; foo="foo"})

would output:

> 3

and:

> print(numentries{foo="foo", bar="foo", baz="not foo!"})

would also output:

> 3

This could just be a simple get on an integer in the backing C struct. This neatly sidesteps the issue of whether a table is a proper sequence, since this doesn't address that question directly. I just want to know how many things there are in this table, at any key. This answers the question that you'd normally have to do something like the following to answer:

> local n = 0
> for k in pairs(t) do n = n+1 end

This being the case, most of the gotchas mentioned above seem to evaporate, and the only reason not to do that that I can see is that it makes *ALL* table writes a tiny bit more expensive. Probably something I'd implement as a power patch, if I was writing a lot of code that would benefit greatly from this.

On Mon, Jun 1, 2015 at 1:36 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2015-06-01 20:59 GMT+02:00 Andrew Starks <andrew.starks@trms.com>:

> Also, I thought that "#" was changed to stop at the first nil in 5.3.

Not "#", but ipairs. The change you remember happened during the
release candidates. ipairs would continue up to #t, even if there is
a hole, and that was changed.




--
Brigham Toskin