lua-users home
lua-l archive

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


2014-08-02 1:53 GMT+02:00 Steven Degutis <sbdegutis@gmail.com>:
> Personally, I do not use most of the metamethods. In fact, I strongly
> recommend against them to users of my application, and authors of
> third party extensions for my application. The only two I ever use are
> __gc for userdata, and __index in the simple example of foo.__index =
> foo and setmetatable({}, foo). I find everything else to not only be
> unnecessary, but sometimes even prohibitive.

Tastes differ. I prefer setmetatable({},{__index=foo}).
It helps me remember that a metamethod is invoked
only when Lua does not know what to do. The old name
was fallback.

I agree that indiscriminate use of metamethods is bad.
But as a systematic way of providing expression syntax
for objects and userdata, they are immensey useful as long
as there is a proper analogy. Then they draw on years of
experience and exploit your intuition..

The prime example for Lua users is LPeg. The analogy is
not perfect (e.g. addition is not commutative) but is close
enough to be suggestive.