This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

how to get unlimited args in a function

Started by Exerro, 26 December 2012 - 01:27 PM
Exerro #1
Posted 26 December 2012 - 02:27 PM
Im trying to make a function where you can input as many args as you want and i think its functionname(…) but im not sure how to access the args after that…can someone help?
Lyqyd #2
Posted 26 December 2012 - 02:48 PM

function doFunc(func, ...)
  local args = {...}
  for i = 1, #args do
    func(args[i])
  end
end
ChunLing #3
Posted 26 December 2012 - 06:12 PM
That is valid, and more consistent with how arguments are passed to the program as a whole, but for a function inside of a program, you can just use the table arg without needing to assign it or anything.
function doFunc(func, ...)
   for i = 1, #arg do
    func(arg[i])
  end
end
Lyqyd #4
Posted 26 December 2012 - 06:34 PM
You can indeed. That method seems to have too much of a flavor of "magic" to it for my tastes, though. I much prefer "wasting" a line on an explicit definition over using a "magic" variable when taking variadic arguments.
KaoS #5
Posted 26 December 2012 - 06:57 PM
You can indeed. That method seems to have too much of a flavor of "magic" to it for my tastes, though. I much prefer "wasting" a line on an explicit definition over using a "magic" variable when taking variadic arguments.

I agree… I prefer to make my code slightly more verbose if it means that it is not subject to the whims of the Lua updaters, you never know if stuff like that will change
ChunLing #6
Posted 26 December 2012 - 11:43 PM
Meh, don't know why they would change that more than they'd change the meaning of any other token. I do take the point that the arg identifier has a bit of an odd place in Lua. For some reason, it's described as a "hidden parameter". I guess because you write the parameter as … then access it as arg (or by assigning something else to {…}).