typestar

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

  1. { ... } gathers the varargs into a table.
  2. select(i, ...) reads one without building anything.
  3. A bare ... forwards everything to the next call.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Functions, step 13 of 27 in Language basics.

← Previous Next →