Varargs in Lua
... collects arguments and forwards them untouched.
-- ... collects arguments; tables and select unpack them.
local function party(...)
local members = { ... }
print("Party of " .. #members .. ":")
for i = 1, select("#", ...) do
print(" " .. (select(i, ...)))
end
return ...
end
party("Wren", "Moss", "Tilda")
-- Varargs forward untouched with a bare ...
local function announce(...)
print("Entering together:", ...)
end
announce(party("Wren", "Moss"))
How it works
{ ... }gathers the varargs into a table.select(i, ...)reads one without building anything.- A bare
...forwards everything to the next call.
Keywords and builtins used here
announcedoendforfunctionlocalpartyprintreturnselect
The run, in numbers
- Lines
- 17
- Characters to type
- 403
- Tokens
- 94
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 87 seconds.
Step 3 of 3 in Functions, step 13 of 27 in Language basics.