This text is from a post I made earlier today to the IRC channel and Twitter. The fact that lua_getfield does not return a value make it cumbersome to error check without making code ... well ugly to be honest. It was suggested the opt_* functions could be used yet that has the possibly of throwing an error which is not what I would like. I realise this function is very easy to implement myself or for any other user yet I request it be considered for including in the luaL section of the library.
Similar to luaL_getmetafield.
Returns 1 if the value for the key denoted by e is of the correct type and the value is left on the stack, else returns 0 and leaves the stack as it was.
obj: location on the stack (or pseudo index) at which to look for the key value pair.
e : String key to look for.
type: One of LUA_TTABLE, LUA_TSTRING et al.
int luaL_getfield(lua_State* L,int obj,char const* e,int type);
if( luaL_getfield(L,'bar',index,LUA_TNUMBER) ) foo = lua_tointeger(L,-1);
otherwise using lua_getfield
if (lua_type(L,-1) == LUA_TNUMBER) foo = lua_tointeger(L-1);
The function could also could not take a type int and instead just return if the value is not LUA_TNIL.
if( luaL_getfield(L,'bar',index) && lua_type(L,LUA_TNUMBER) )
foo = lua_tointeger(L,-1);