lua-users home
lua-l archive

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


On 12.04.2014 10:10, Dirk Laurie wrote:
2014-04-12 5:02 GMT+02:00 Peng Zhicheng <pengzhicheng1986@gmail.com>:

the `metamethod' mechanism in Lua is FALLBACK, not OVERRIDE.
the __index metamethod is fallback for non-existance keys.

This does not apply to userdata, which is what I use.
I use metatables mostly like this:

 Step 1: Do some useful stuff in c/c++.
 Step 2: Represent it in Lua via Userdata+Metamethods.
 Step 3: Use it in fairly small scripts.
         If matters get too complex, go back to 1.

I always need __index.
Artificially limiting its capabilities is wrong. For me.

This can't be stressed often enough. It is often misunderstood by
newbies. I can remember how long it took me to grok this. Not
only table indexing, also the "clever" things you can do with
debug.setmetatable, e.g. __concat for numbers (metamethod
not called because they are coerced to strings), __add for strings
(metamethod called only when they can't be coerced to numbers).

This sounds like you've been carried away and hit your head
trying all sorts of strange things that would never occur to me.

Dirk