lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 28-Jun-07, at 7:39 PM, Tim Hunter wrote:

I have a varargs function. What is the Lua idiomatic way of determining if all the arguments are nil? I've thought of two ways:

1. #{...} == 0

That won't work reliably. Try it on #{nil, true, nil}

2. next{...} == nil

That's probably the fastest.

This function is called very frequently, so I'd prefer not to have to create a table if I can avoid it.

Perhaps you need to rethink your design, then :) It's hard to do
non-trivial things with varargs in Lua without turning them into
a table. About the only other useful thing you can do with them
is pass them to another function.

If you knew the function would never be called with more than
three arguments (say), you could write it as a fixed arg
function:

  function foo(a, b, c)
    if (a or b or c) == nil then
      -- they're all nil
    else
      -- something's in there somewhere
    end
  end