Greetings, developers.
I just wanted to know if it’s possible to know how many arguments does a function takes.
Example:
local foo = {
["Example_1"] = {id= 1, run= function(arg) print(arg) end},
["Example_2"] = {id= 2, run= function(arg, arg1) print(arg, arg1) end}
}
foo[input].run("Hello") -- ???
foo[input].run("Hello", "World") -- ???
My apologies if the post isn’t in the correct category, this is my first post.
1 Like
aliaun125
(David)
August 20, 2022, 12:05pm
#2
You can take as many arguments as you want
local x = {}
for i = 1,5000 do
table.insert(x,i)
end
function my(...)
print(#{...})
end
my(table.unpack(x)) --prints 5000
Okay, but how would I use the arguments? Like
function my(...)
print(...[3])
end
???
1 Like
aliaun125
(David)
August 20, 2022, 12:27pm
#4
the … let u take as many arguments as u want
function x(a,b,...)
print(a,b,...)
end
x(4,"hi",12,true) --prints 4,"hi",12,true
TenBlocke
(Decablocks)
August 20, 2022, 12:29pm
#5
… is just a little trickery with function arguments so that you can get any type of argument
local function foo(...)
print(...) -- true, false, 12345
local tbl = {...} --stored tuple arguments as an array with start to finish
--useful for iterating over an unknown amount of objects and all you got are tuples
end
foo(true, false, 12345)
1 Like
It was kinda hard, but I finally figured it out, thanks!
Forummer
(Forummer)
August 20, 2022, 1:34pm
#7
local function f(...)
print(select("#", ...))
end
f("Hello world!", math.pi, math.huge) --3
No table manipulation required.
1 Like