lua-users home
lua-l archive

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


Rici Lake wrote:
(...)
>   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


Michael Broughton wrote:
(...)
function f2(...)
   for i = 1, select("#", ...) do
       if select(i, ...) ~= nil then
           return false
       end
   end
   return true
end


Perhaps both?

function emptyquad( a, b, c, d )
    return ( a or b or c or d ) == nil
end

local s = select
function emptyn( ... )
    for i = 1, s( "#", ... ), 4 do
        if not emptyquad( s( i, ... ) ) then
            return false
        end
    end
    return true
end

function foo( ... )
    if emptyn( ... ) then
        print("empty")
    else
        print("not empty")
    end
end

The good thing about it is that (IMHO) most functions receive less than four parameters(*), so you won't waste too much with function calls.

I myself would make an C function for this:

int l_emptyn( lua_State *L)
{
    int i, t = lua_gettop( L );
    for ( i = 1; i <= t; i++ )
        if ( !lua_isnil( L, i ) ) return 0;
    lua_pushboolean( L, 1 );
    return 1;
}

--rb


* If it is not your case, you cold tune to your heart's content.