Codectionary / Developer documentation / Lua

Lua nil-preserving argument lists

Lua 5.2+ table.pack stores an explicit argument count in n. Passing that bound to table.unpack preserves holes and trailing nil arguments.

Syntax

local args = table.pack(...); callback(table.unpack(args, 1, args.n))

Examples

Lua nil-preserving argument lists

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local args = table.pack("Ada", nil, "editor", nil)
local function inspect(...)
    print(select("#", ...))
    local name, missing, role = ...
    print(name, missing == nil, role)
end
inspect(table.unpack(args, 1, args.n))
  1. Output: 4, then Ada, true and editor. n preserves the final nil argument even though tables cannot store nil fields.

Best practices

  • Do not use #args for lists with nil entries. The n field is metadata and does not update itself after mutation.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading