lua-users home
lua-l archive

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


what about switch case?

switch (lua_type(L, 2)) {
  case LUA_TNUMBER:
  ...
  case LUA_TSTRING:
  ...
  default: type_error(L, 2, "number/string");
}

2014-08-02 4:30 GMT+08:00 Sean Conner <sean@conman.org>:
> It was thus said that the Great Coroutines once stated:
>> On Fri, Aug 1, 2014 at 12:57 PM, Sean Conner <sean@conman.org> wrote:
>>
>> >   One situation I hit in C was the following:
>> >
>> >         static int foobar___index(lua_State *L)
>> >         {
>> >           foobar__s *foo = luaL_checkudata(L,1,TYPE_FOO);
>> >
>> >           if (lua_isstring(L,2))
>> >             ...
>> >           else if (lua_isnumber(L,2))
>> >             ...
>> >         }
>> >
>> > Odd bugs until I actually read the description for lua_isstring():
>> >
>> >         Returns 1 if the value at the given index is a string or a number
>> >         (which is always convertible to a string), and 0 otherwise.
>> >
>> > lua_to[l]string() will do a convertion if the target is a number.  I'm sure
>> > it's convenient in some cases, but not in others.
>> >
>> >   Oddly enough, I dont' have an issue with lua_isboolean() or
>> > lua_toboolean() even though they do a conversion as well (nil or false as
>> > false, anything else is true).
>>
>> I think I've just made it a habit to use luaL_checktype(), I can see
>> how that might be confusing to use the is<type>() functions, though
>
>   luaL_checktype() throws an error though.  Here, I'm checking the same
> index for different types.  I suppose I could have done:
>
>         if (lua_type(L,2) == LUA_TNUMBER)
>           ...
>         else if (lua_type(L,2) == LUA_TSTRING)
>           ...
>
>   -spc
>
>



-- 
regards,
Xavier Wang.