Awesome info Drake, that's kind of the example I was trying to come up with, variable args, but after a few minutes of not getting the results I was looking for, I thought I'd ask the experts. :-)
Sent from my new BlackBerry Z10
Quoth Coda Highland <email@example.com>, on 2013-02-14 18:06:46 -0800:
> > This is not true in general, though it's true much of the time.
> > In a context that expects exactly one value, such as __index
> > return values, returning nothing gets adjusted to a single nil.
> > So they are the same in this case.
> With the exception of MULTRET in the C API, what cases am I forgetting about?
Variable number of return values is possible in Lua itself as well. In
particular, a function call at the tail of an argument list will
have all of its returned values passed as arguments. Imagine:
function paranoid_format(fmt, ...)
if compute_format_arity(fmt) ~= select('#', ...) then
if foo1.test(key) then return foo1.result(key) end
if foo2.test(key) then return foo2.result(key) end
-- No explicit nil return!
-- Later that night...
-- Kaboom! paranoid_format gets zero varargs rather than one.
The same thing applies to the tail end of a return _expression_, which means
that the number of return values propagates upward through the call stack
when one function returns the result of another.
You can prevent this by using a second pair of parentheses to explicitly
adjust the number of results to one. In most positions (RHS of assignments,
return value thrown away, most nested _expression_ positions), the number of
results is implicitly adjusted, usually to one---but if you're designing
an API in Lua, for instance, be clear about whether you rely on this from
The other case of variable return count is table constructors, but it
doesn't matter in that case because trailing nils in a table constructor
do nothing. :-)
---> Drake Wilson