lua-users home
lua-l archive

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


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.
...
> metamethods are not C++'s overloaded.operators.

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).

Maybe we should acquire the habit of changing the name when doing
the local caching.

    local setfallback = setmetamethod