Hello
In the example below, ... returns only the first argument correctly and the rest is nil. Why is that happening?
local function dd(a1,a2)
print(a1,a2); -- true,nil
end
function(bool,...)
local Arguments = ...; -- I could make a table out of this, but I don't want to achieve such goal, i want to have multiple arguments in the `dd` function
if bool then
dd(ArgumentsForAnotherFunction);
end
end
If someone could tell me more about the ... (and most importantly how is it called), then it would be great and I would really appreciate it.
I believe people refer to them as concatenations, but I don’t quite remember if that is the correct name to the top of my head. People call them different things in different programming languages. They are basically used to combine two strings together. So lets say we have a variable called “response” which has a sentence in it and we want to print that out with another word. Here is what we would do.
local response = "This is a response"
print(response..." that is cool") -- the concatenation helps combine those two strings together.
expected output: This is a response that is cool
This is also my first time knowing this thing. I thought the 3 dots are for some example that doesn’t do anything that’s why I don’t pursue learning it.
Thanks for this! Now my question is, Is this a good way to use when developing something? I mean is this really helpful? Sorry if I’m asking so stupid, it’s just I don’t know how,when to use this.
Yeah they can be pretty useful, one way I use them is to test out remote events.
Just do function(…) Print(…) End and it’ll print all the parameters for quick easy testing. I believe there are more that I have used but I forgot. I’ll edit the post later maybe.
Thanks, however the number of arguments may vary, and I have a type of system where I don’t know how many arguments are being parsed. How can I solve it?
For this issue the only reason the first argument is passed is because well, you only defined one variable here is what it would look like without the (…) notation
local Arguements = arguement1,arguement2,arguement3
--yeah arguement2 and arguement 3 just go poof
One way to get around this is to pack them like what @ObserverBot has done. Or you can use table.pack()
local Args = table.pack(...);
Yep what @twinqle said would work for non-nil arguments but, keep in mind if an argument is nil then it wouldn’t be counted.
Here’s a special thing that table.pack does which gets the number of arguments in the (…) notation, if you wanted to.
local test = table.pack(1,2,3,4,nil,6)
print(test.n) --6 arguments
Edit: Yeah for the code sample given, to easily pass all the arguements to the function dd just pass … to it, no need to table.pack() if you wish to forward the arguments in the same order.
Another example of the usage of … is the advanced debounce function which makes any function inputted into it able to have a debounce.
local function dd(...)
local a1,a2 = ...
print(a1,a2); -- true,nil
end
Keep in mind the rest of the arguments will be thrown away, it might be better to print(…) instead so it supports all the variables inputted which is the purpose of … variadic functions.