Some Lua functions return nil
to signal the user that the function couldn't carry out some task (e.g., tonumber()
, string.find()
).
In C, returnig nil
is done like this:
int some_function(lua_State* L) {
...
if (some condition) {
lua_pushnil(L);
return 1;
}
...
}
HOWEVER, I wonder if it's alright to do the following instead:
int some_function(lua_State* L) {
...
if (some condition) {
return 0;
}
...
}
It's shorter. I tried it and it seems to works, but I don't know if that's by-design. I examined Lua's source code and I don't see this return 0
pattern so I wonder if it's legit to do this.
Are the two different ways to return nil
equivalent?
(BTW, I know all about signaling errors via exceptions (that is, lua_error()
) so please don't mention it.)
UPDATE:
I now see that there's a subtle difference between the two methods: print((function() end)())
would print nothing whereas print((function() return nil end)())
would print "nil". I don't know how important this is.