lua-users home
lua-l archive

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


Hallo,

Vincent Penne wrote:
 > But with lua 5, there aren't any more tags, so how do I check easily
what is the type of my userdata ? I know I could look into the methatable for some custom entry that I would call "type" for example, but it looks overly complicated and inefficient. Furthermore, there aren't any unified way to generate new type values (like the lua_newtag function) so two libraries may accidentally generate two different kind of userdata with the same "type" value in their metatable, so it would not be safe either. Probably I have missed something ...


You can say two objects (userdata or tables) are of the same type if they share the same metatable. If want to refer to your types by name, you can create entries in the global namespace using the type name as a key and the metatable as a value. I guess luaL_newmetatable and luaL_getmetatable do just that.

Another question : I see that there are light userdata, which are much like the userdata in lua 4, except that they don't have tags. The documentation doesn't seem clear about these light userdata, can I associate a metatable to them or not ? If not, how can we use them since there are absolutely no way we could test the type of these light userdata without metatable nor tag.


You can't associate metatables with light userdata, but they are useful in other contexts. The I/O standard library stores FILE* pointers in light user data.

Now that Programming in Lua is online (http://www.lua.org/pil/), it's definitely a excellent read for anyone studying Lua.

-alex