lua-users home
lua-l archive

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


On 12/14/2012 06:30 AM, Dirk Laurie wrote:

local function merge(...)
    local s, arg = {}, {...}
    local n = select('#',...)
    for j=1,n do if type(arg[j])=='table' then
       for k,v in pairs(arg[j]) do s[k]=v end
    end end
    return s
end

Slightly off-topic, but... my testing indicates that replacing:
    local n = select('#',...)
with:
    local n = #arg
generates smaller code that looks to be more efficient, using both 5.1 & 5.2 luac. Are they not equivalent?

My test code:
-- luac -l -o /dev/null test-vararg.lua

function va1(...)
   local arg = {...};
   return arg, select('#',...);
end

function va2(...)
   local arg = {...};
   return arg, #arg;
end