lua-users home
lua-l archive

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


function test(a,b,c,d,e,f,g,h,i,j,k,l,m,n)
   ..........
end

can be called with just test(4,3) then the rest of the parameters are nil in
the function

/Erik


----- Original Message -----
From: <cmuz@gmx.de>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Friday, November 10, 2000 1:04 PM
Subject: [novice-question/suggestion] vararg propagation


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