[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: delegation
- From: Edgar Toernig <froese@...>
- Date: Fri, 17 Aug 2001 04:51:37 +0200
Ignacio Castano wrote:
>
> Hi, i have some kind of mixed object hierarchy in lua and c, where
> objects and classes are tables.
>
> I can overwrite a method in the hierarchy, but to call the overwritten
> method i have to do something like this:
>
> luz = TiLight {
> SetColor = function(self, r, g, b)
> self.parent.SetColor( self, r, g, b )
> end,
> }
Unfortunately, this only works when there is only one parent. If you have
a chain of parents this does not work. I.e. if there exists
self.SetColor
self.parent.SetColor
self.parent.parent.SetColor
The first SetColor does your way of calling the base class's SetColor.
If that one tries the same, it has to call self.parent.parent.SetColor else
it will just call itself recursively.
>[...] This code is going to be written by non programmers so i would like to
> simplify it a bit, maybe using a function call like:
>
> self:Delegate( r, g, b )
>
> but, can i get the name of the function that i have to call? any other ideas
> on how to simplify that?
You not only need the name of the function (and no, Lua will not give it to
you, you have to give it to Delegate explicitly), you also have to give it
a reference of where in the chain of parents to start searching.
I got the same problem in Sol's class system. I found no easy automatic way
to handle it. At the moment I'm simply stuck to call the base class's method
directly. I.e: (Sol constructs)
SomeNewClass = SomeBaseClass:class { -- derive a new class from SomeBaseClass
...
function SetColor(self, r, g, b)
...
-- call lower SetColor; start searching for the method at SomeBaseClass.
SomeBaseClass.SetColor(self, r, g, b)
...
end
...
}
In your example: let's say TiLight's parent is FooLight:
luz = TiLight {
SetColor = function(self, r, g, b)
FooLight.SetColor(self, r, g, b)
end ...
This will of course only work if you have a static class hierarchy (that is,
TiLigh.parent is always FooLight).
Ciao, ET.