lua-users home
lua-l archive

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


On Fri, Jul 08, 2016 at 12:36:30AM +0200, Dirk Laurie wrote:
> 2016-07-07 23:36 GMT+02:00 William Ahern <william@25thandclement.com>:
<snip>
> > But for a non-sequence, #t might evaluate to a lesser index
> > after executing t[#t + 1] = true. What use is the guarantee that
> > t[#t + 1] is empty if you lose track of that value after the fact?
> 
> Suppose that t represents the rooms in an hotel. Guests arrive
> and leave. You evaluate #t. It tells you where to find an empty
> room. You put a guest in that room. Next guest comes. Again
> #t finds an empty room. Why is it bad if that room happens to
> have a lower number? But it would be bad if the guarantee
> that t[#t+1] is empty did not hold.

So you've put them in the room. Then what? You can't find them again. You
can't do `for i=1,#t do ... end` and expect to find them.

You could remember the index somewhere, but then you've only created a level
of indirection. Wherever you store the index you could have kept the object
itself. You if you wanted to anchor the object in memory, it would be
simpler and more performant to store it as the key.

Hmmmm... I guess one use would be for implementating luaL_ref. I'm not sure
how useful that would be in Lua code. Maybe I just need to think about it
longer.