Re: Parsing varargs to a function
Lars Müller <[email protected]> Mon, 27 Apr 2026 13:57:35 -0700 (PDT)
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
Sainan: To be precise, it's actually t.n rather than #t. This makes some sense because #t generally may stop at any boundary. See also https://lua.org/manual/5.5/manual.html#3.4.10. Worth noting that vararg tables, in 5.5, are guaranteed to have the actual "table.pack" optimized away if you only index them and take their count t.n (directly, not in a closure), because then Lua can just peek at the stack to get the values. Personally I would have preferred a (peephole-ish) optimization that makes "x = select(i, ...)" constant time, as found in LuaJIT. Then the "select"-loop would be fine as idiomatic vararg iteration construct. (In its current state, it runs in quadratic time on PUC Lua, as varargs with (average) linear length are copied on the stack in every invocation. For older versions of PUC Lua, I thus recommend iterating tables by first packing them up using table.pack and then iterating up to t.n instead.) - Lars On Monday, April 27, 2026 at 10:42:45 PM UTC+2 Sainan wrote: > May I introduce you to Lua 5.5? > > function f(...t) > for i = 1, #t do > print(t[i]) > end > end > > -- Sainan > -- 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/3865463c-c734-4ae8-b1d0-5b37e5c14853n%40googlegroups.com.