lua-users home
lua-l archive

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

2017-10-31 13:32 GMT+02:00 Ulrich Schmidt <>:
> 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]:

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 "
      " defined for "..record.tag)
    return mm(record,...)

-- metamethods for ~ + - to be supplied per tag
for mm in ("bnot,add,sub"):gmatch"%l+" do
  mm = '__'
  RECORD[mm] = metamethod(mm)

`record.tag` is the name used as key in `meta` for the OBJ's.