lua-users home
lua-l archive

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

Mark Hamburg dixit:

> >> I think what Denis is asking is why we can't just do things like put
> >> __tostring in the object/table itself and instead we have to put it in
> >> the metatable.  
> > 
> > You can. Just set the table as its own metatable.  
> Good point. I haven't seen that pattern all that often, but it certainly does work and it avoids the need to create lots of pieces to build an object.

Right! (I like this kind of recursive schemes.) But in the (common) case of a subtype or a single object inheriting some of its behaviour, it seems this pattern cannot apply.
What if I override only __call, __add or __tostring? I have to let other keys point to their original target, and only change the keys to overriden metamethods. So, I guess the only solution is to copy the metatable and write needed changes (by hand).
Unless _all_ metamethods are systematically copied to new objects -- but I haven't yet seen this kind of (non-)inheritance. This means all objects carry, in particuliar, (references to) what is defined in the OO framework's root object, which can be heavy.


la vita e estrany