lua-users home
lua-l archive

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


Wolfgang Pupp <wolfgang.pupp <at> gmail.com> wrote:

 

>I like this idea a lot! How does this work, exactly- if an object already

>has an __index function, is a lookup for a generic method only performed

>when the __index- function returns nil (errors?), or not at all?

 

The mechanism is a bit simpler than that. There is still 'really' only the object metatable. The type metatable pointer is copied from the Lua state into the object automatically on object creation (so think of it as a default). A newly created object uses the type metatable, but you can replace this for the object using setmetatable. You can even disable the type metatable for a specific object using setmetatable(tab, nil). getmetatable(tab) immediately after object creation will return the type metatable allowing you to delegate to it from the '__index' metamethod of your own object metatable if that's what you want to do.