lua-users home
lua-l archive

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


Hello,

I just started to play with Lua 3.2.2.
I like it very much but need some help:

Is there an __easy__ way to propagate a variable number of
arguments to subfunctions without getting nested
argument tables ?

simplified example:

a,b,c = 1,2,3

function sum( ... )
local i=1
local a=0
  while i <= arg.n do
    if type(arg[i]) == 'number' then -- check type to avoid errors
      a = a + arg[i]
    end
         -- recursion if type(arg[i])=='table'
         -- would work, but it´s not very nice.   
    i=i+1
  end
return a
end

function foo( ... )
-- do something important with args
return sum( arg ) -- return sum
end

print(sum(a,b,c))  -- 6
print(foo(a,b,c))  -- 0 

-- You must deal with nested tables in 
-- function 'sum' to get the same result

suggestion:
May be a notation like %... or %arg could stand for
flat varargs propagation ?!

CM