lua-users home
lua-l archive

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


DISCLAIMER: I'm just wondering about this topic

table.pack() is used to pack "the stack", that can hold nils. To accomplish this behaviour it uses the  ".n" field (I'm not analysing the merit here). Since this is somewhat arbitrary, look this example:

table.packs = function(...)
        local len = select('#',...)
        return setmetatable({...},{
                        __len = function() return len end,
                        __newindex = function(t,k,v)
                                if math.type(k) == 'integer' then
                                        len = math.max(len,k)
                                end
                                rawset(t,k,v)
                        end
                })
end

that returns an effective sequence (defined by #) compatible with "the stack", even if ... initiate/terminate with 'nils'. This is symmetric with table.unpack and all table.something() that use #  behaves as expected. (am I wrong?)

Why not use __len instead of ".n"?

--
Rodrigo Azevedo Moreira da Silva