lua-users home
lua-l archive

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




On 24/07/16 10:58 PM, Rodrigo Azevedo wrote:
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

Well obviously it's because you're not supposed to use it.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.