lua-users home
lua-l archive

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


nil is indeed not a string. It’s a special value.

if ( lua_isnil (L, -1))
               // foo did not return a string
else if (lua_isstring (L, -1))
               // foo did return a string

should do the trick.

2010/1/19 Paul Beckingham <paul@beckingham.net>:
> I am calling a Lua function from C.  That function looks like this:
>
>        function foo ()
>          -- stuff omitted
>          return 1, nil
>        end
>
> foo returns two values, an integer and a string (or nil).  In the C code, I want to check to see if the second value is nil or a string.  Here is what I thought I needed:
>
>        // the Lua code above is loaded.
>
>        lua_getglobal (L, "foo");
>        lua_pcall (L, 0, 2, 0);
>
>        if (! lua_isnumber (L, -2))
>                // foo did not return a number
>
>        if (! lua_isstring (L, -1))
>                // foo did not return a string
>
> The nil value is failing the lua_isstring test, presumably because nil is not a string.  How do I check for nil, otherwise a string?
>
> Thanks.
> Paul.
>
>