lua-users home
lua-l archive

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




On Saturday, April 18, 2015, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great Tim Hill once stated:
>
> >>
> >> I would doubt that using varargs as a Forth stack is the best approach. What’s wrong with a Lua array (aka table)?
> > Lua arrays can't do nil, Lua stacks can.
>
> Well, it’s true that a “true” Lua array (a sequence) cannot contain a nil value, but if you maintain your own top-of-stack (aka array size), then there is no reason why you cannot have nil as the value of some elements…
>
> t = { “a”, “b”, “c” }
> t.len = 3
> t[2] = nil

  And in Lua 5.2:

t = setmetatable(
        { _len = 3 },
        { __len = function(self) return self._len end }
)

print(#t)

  -spc


Lua doesn't have nil, inasmuch as nil in a sequence does not work. However, you would almost certainly get better performance by overloading __new/index and storing length in `.n`, like old Lua did, and then adding __len to read it. The other approach is to use a sentinel, but that's not false-y, if that matters. 

I'm not only poking my nose in to state the obvious. I have a similar issue in my system, where I need to "delete" keyframes and nil can't be used and false is a legal value. So, I use a delete method, instead. I may have used NUL sentinel, but it seemed unLua like, to me. 

varargs are the way they are because they were simple to implement in Lua, and people didn't like throwing a table away on every function call that had varargs. My sense is that their existence eats away at Roberto ( not being overly serious, but I say this based on the 2014 talk that he gave).

The way I see it: I use varargs all of the time, don't want to see them deprecated, and so I keep my fool mouth shut about them, and just accect them as they are. ;)

Rock on though. In the beginning of this thread, I was going to guess "cannabis use" but didn't know how to write it without sounding like a jerk or a pot head. 

-Andrew