Re: Parsing varargs to a function

'Lars Müller' via lua-l <[email protected]> Mon, 27 Apr 2026 22:48:58 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
Hehe, I have a WIP blog post about this :-)

I used to think that the "select loop" is the preferred way to iterate 
varargs too. It has the desired semantics: It iterates nils, and it's 
simple enough.
Unfortunately, it's only fast (linear time) on LuaJIT.
On PUC Lua, it's quadratic time in the length of the vararg, because a 
whole lot of copying happens on the stack.

That is, the "proper" way to iterate a vararg in PUC Lua is to pack it 
up in a table t with table.pack, and then iterate that up to t.n using 
numeric for.
(On 5.1, you need to polyfill table.pack using select("#", ...) but 
that's okay.)

Lua 5.5 finally adds a proper, more efficient way with vararg tables, 
see <https://lua.org/manual/5.5/manual.html#3.4.10> for details.
Effectively your example becomes:

function f(...t)
  for i = 1, t.n do
    print(t[i])
  end
end

This is guaranteed to be optimized to just indexing the stack (and 
recording the number of items on the stack at calltime), so no garbage 
table is actually created and populated, unless t is used in a manner 
that makes this optimization inapplicable (e.g. it may escape as an 
upvalue, or further keys are added to it, etc).

Personally I think I would have preferred select + truncation being 
peephole optimized as in LuaJIT in order to avoid the need for an 
additional language feature, but at least there's now a "canonical" way 
to iterate varargs in PUC Lua :)

- Lars


On Mon, Apr 27 2026 at 16:31:12 -04:00:00, Sean Conner 
<[email protected]> wrote:
> 
>   I know that to parse varargs, one uses select():
> 
> 	function f(...)
> 	  for i = 1 , select('#',...) do
> 	    local x = select(i,...)
> 	    print(x)
> 	  end
> 	end
> 
>   What was the thinking behind this?  Why not?
> 
> 	function f(...)
> 	  for i = 1 , #... do
> 	    local x = ...[i]
> 	    print(x)
> 	  end
> 	end
> 
>   That is, treat ... as an array and use existing syntax?  I know 
> this will
> not work with ipairs(), but it seems to be a net win.  Currently, 
> with an
> empty Lua state, there is no way for code to interate through the 
> varargs
> without the select() function (or at least, not easily).
> 
>   -spc (Just thinking out loud here ... )
> 
> 
> 
> 
> --
> You received this message because you are subscribed to the Google 
> Groups "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, 
> send an email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion visit 
> <https://groups.google.com/d/msgid/lua-l/20260427203112.GA31146%40brevard.conman.org>.

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/M576ET.QJKE5Y9D8YSV3%40gmx.de.