lua-users home
lua-l archive

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



On Aug 27, 2005, at 09:27, Rici Lake wrote:

Sure, I understand that. I used to provide objects that didn't use the : syntax at all, despite the general inefficiency. I stopped doing it because it gets to be a pain.

I have to admit that after 6 months of cogitation I have learned to love the colon as well. Smooth sealing since then :)

That said, I still have one fundamental issue:

- How to properly handle invocation of parent's methods?

Quite often, I want to simply extend a method implementation, not overwrite it entirely.

For illustration purpose, lets assume I have a DAVService class to handle WebDAV requests. DAVService extends ObjectService, which in turn extends Service. Each class provides an initialization method which needs to be properly chained. In other words, a subclass needs to invoke its parent implementation. Something along these lines:

-- DAVService initialization
function self:init( aPrefix, aPath, anAuthenticator )
self:invokeSuper( ObjectService, "init", aPrefix, anAuthenticator )

        self._path = aPath

        return self
end

-- ObjectService initialization
function self:init( aPrefix, aType, anAuthenticator )
        self:invokeSuper( Service, "init",  aPrefix, anAuthenticator )

        self._type = aType
        self:services():put( aType, self )

        return self
end

-- Service initialization
function self:init( aPrefix, anAuthenticator )
        self._prefix = aPrefix
        self._authenticator = anAuthenticator

        return self
end

Note the quirky 'invokeSuper' method, which lookups a given class method and invokes it:

function self:invokeSuper( aSuper, aMethod, ... )
        local aFunction = aSuper[ aMethod ]

        return aFunction( self, unpack( arg ) )
end

Note how I need to pass the effective super class to the method. This is the crux of the problem as it requires the entire inheritance chain to be spelled out explicitly instead of simply using something like self:super():doIt().

Haven't cracked this one yet :/

Sigh...

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/