lua-users home
lua-l archive

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


Am 28.03.20 um 20:47 schröbte Kalafut, Bennett:
We were surprised because

self.__index=self
setmetatable(derived,self)

is so often written of as implementing inheritance, without caveat.    The reference manual is approximately the only place where it is noted that metamethod queries are raw.

The above won't inherit metamethods even if metamethod queries were non-raw because the metatable doesn't have a metatable of its own.


I don't know when a new PIL is coming out but perhaps there's an opportunity in the treatment of OOP to show (and establish an idiomatic way of doing) what must be done to implement inheritance of __newindex, __call, and operators.  Overrides aren't clean without doing custom metatables.  We don't have to worry about that in our application but it means that setmetatable(derived,self) is not quite as universal as one would think.

Some operators are specific to the type they are implemented for, e.g.:

    function MyNumberMeta.__add(a, b)
        return MyNumber.new(a:getRaw() + b:getRaw())
    end

So it wouldn't make sense to inherit those.


-BSK

Philipp