I was making a script to get the largest value out of an unknown number of arguments, but I can't really figure out how to use (...) tuple.
Can someone explain how it works, please? The only thing I know is that it's not a true tuple, since lua actually treats them as individual values and not a data structure like a table or such.
Thanks!
local function Val(...) print(...) end Val(1, 2, 3, 4, 5, 6)
1, 2, 3, 4, 5, 6
The code isn't really that important to the question, Its just what I first was messing around with.
Alright. Lua can be pretty confusing, especially in the more sketchy nooks and crannies that you don't really mess with until you somehow do. This is one example.
The ...
argument is actually pretty straightforward. It does what you think it does - makes it possible for the function to take in any number of arguments. However, accessing those arguments is less-known but painfully simple. Here's what you do:
local function Val(...) local arguments = {...} -- This line is key! -- Do whatever with the arguments end
Surrounding ...
with braces converts it to a table of arguments. The first argument would be at position [1]
, the second at [2]
, etc. etc.
That's it!
(Don't ask me why its type is a number. I don't know either.)