[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Metamethods and inheritance
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 31 Oct 2017 15:29:02 +0200
2017-10-31 13:32 GMT+02:00 Ulrich Schmidt <u.sch.zw@gmx.de>:
> I suggest, you take a look into middleclass[1]
> how they do. But "__index"-ing from one table to the next to the next to the
> next .... is time consuming, so take care.
>
> [1]: https://github.com/kikito/middleclass
OK, I have looked, and unless I totally misunderstand the code, the
parent class knows which subclasses it has, and propagates missing
methods into them.
I've designed a different mechanism, but it also involves keeping a
table of subclass metatables.
local metamethod = function(name)
return function(record,...)
local mmt = meta[record.tag]
local mm = assert(mmt and mmt[name],"No metamethod "..name..
" defined for "..record.tag)
return mm(record,...)
end
end
-- metamethods for ~ + - to be supplied per tag
for mm in ("bnot,add,sub"):gmatch"%l+" do
mm = '__'..mm
RECORD[mm] = metamethod(mm)
end
`record.tag` is the name used as key in `meta` for the OBJ's.