[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: table.maxn...
- From: Roberto Ierusalimschy <roberto@...>
- Date: Sun, 31 May 2015 09:48:11 -0300
> On Sat, May 30, 2015 at 11:28 PM, Brigham Toskin
> <email@example.com> wrote:
> > Does anyone know the rationale for not just automatically tracking the
> > number of entries in a table?
> reliably tracking table size means at some point you need a linear
> scan. Yes, i know at first sight it looks like it's just a +1 or -1
> (or neither) on each table setting, but there's always some
> not-uncommon case where you have to do a potentially-too-heavy
> Table setting operations are too critical for _all_ Lua code to be
> subject to that kind of overhead. Having a logarithmic (and
> frequently resolved at the first step) operation at visible points of
> your code seems a better compromise.
Let us not go into this hole again. The problem is not only cost, it is
conceptual. A key point of '#' is this:
t[#t + 1] = v -- add v to the end of the list
t[#t] -- gives you the last element in the list
t[#t] = nil -- removes the last element of the list
Before proposting any other semantics for #, think whether it supports
these basic list-manipulation operations (insertion, deletion, access
to last), and whether it can *build* lists containing nils using those
operations. If not, the new semantics is not solving the problem.