Using varargs without having . . . as an function argument?

For example,

function myFunction(a,b,c)
  if true then
     otherFunction(...)
  end

  return a * b
end

How would I do this without declaring … as myFunction’s parameter?

Are you sure this is correct syntax? ... cannot be used as an argument, it’s set as a parameter. Can you please what you’re asking about more?

My apologies, I meant parameter. I would like to be able to pass all arguments to another function while still defining parameters a, b, c, etc. I know I can unpack … but I wasn’t sure if there is a simpler way, like shown in my code example.

Oh I see, do you know how many parameter there will be? If so, you can do

function myFunction(a,b,c)
  if true then
     otherFunction(a,b,c)
  end

  return a * b
end

But if you don’t, you would have to use unpack, I doubt there is any other way.

function myFunction(a,b,c)
  if true then
     otherFunction(unpack({a,b,c}))
  end

  return a * b
end

There is another way, using debug.info, which actually returns a function’s parameters, sadly roblox has that disabled

1 Like

I do not know how many parameters there would be. I was hoping there would be something like using debug.info where you can get the parameters of the function. Thanks for your valuable feedback!

You could pack varargs with table.pack and define a, b and c and the first 3 indexes:

local function MyFunction(...)
    local Args = table.pack(...)
    local A, B, C = Args[1], Args[2], Args[3]
    --// Code
end

Also a correction, debug.info doesn’t return the actual values corresponding to the parameter, it returns the actual name of each parameter, so if you had a parameter a, it would return a string "a"

That’s exactly he’s problem I think, he doesn’t want to do

local function MyFunction(...)
--he wants to do
local function MyFunction(a,b,c)

I am aware that this is an XY problem! But obviously the user knew ... existed, he was just wondering if there was another way of doing it.

No need for tables or unpacking!

Following your example…

local function myFunction(...)
    local a, b = ... -- define a, b as the first two arguments

    if (condition) then
        otherFunction(...)
    end

    return a * b
end

Since your function always expects some values for a and b, you can define it like this:

local function myFunction(a, b, ...)
    if (condition) then
        otherFunction(a, b, ...)
    end

    return a * b
end
6 Likes

Huge oversight on my end, makes complete sense to use your second example. Thanks!

1 Like

Sorry, that was not directed at you.

1 Like