lua-users home
lua-l archive

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


on 1/22/05 11:26 AM, André de Leiradella at leiradella@bigfoot.com wrote:

>> The issue I was raising was if A subclasses B subclasses C and each
>> provides an implementation of doIt, then you aren't passing enough
>> information to super for it to do it what it needs. One essentially
>> needs two pointers:
>> 
>> * The instance object
> 
> Isn't it "self"? Every method will get it when called with ":".
> 
>> * The current point in the inheritance hierarchy
> 
> In my class system, this is a _CLASS field found on every instance that
> points to their respective classes, which in turn hold their methods and
> have a _SUPER field.

We can ask self for it's class. We can ask a class for it's superclass, but
you need a way to ask the question: For the method implementation currently
executing what is the implementation in the next class up the inheritance
chain. That means that a method needs to know not just self, but the class
it is implemented in or the superclass of that class.

In my example above, for example, given just self, how would the
implementation of a method in B, find the implementation in C? It either
needs to know that it is part of B or it needs to know that it's superclass
is C.

> 
>> Let's do a Lua version with Base and Derived and some simple syntax
>> along the lines of what's been proposed so far (though I make a
>> distinction between instance methods and class methods).
>> 
>>     Base = class()  -- no other base class
> 
> Shouldn't you create the methods table, i.e. Base.methods = {}?

I assumed that the class() function would do that.

>> Finally, note that these sort of issues also exist in prototype-based
>> and delegation-based systems. We want to know both the
>> object-of-entry into the method chain together with our current point
>> in that chain.  
> 
> Those would be "self" and "self._CLASS".

That only gets you one level. What happens when the method inherited from a
superclass wants to call the method above it?

> 6) Calling inherited methods
> 
> instance:super.method([argument] {, argument})
> 
> I don't see why calling inherited methods from super classes other than
> the direct super class is needed.

The syntax you present isn't legitimate Lua, but I'll ignore that for now.

I haven't been worrying about how one jumps over superclasses in the
inheritance chain. The issue is that one wants self to remain the same
throughout the process so that it can be passed elsewhere and so that we can
call overridden methods.

Returning to the multi-level inheritance hierarchy with implementations for
the same method at each level (using your notation):

    Base = class()
    Middle = class( Base )
    Leaf = class( Derived )

Furthermore, assume we have a method named "method" that we want to
implement at each level and at each level it needs to call the inherited
version.

    function Base:method()
        print "Base method, nothing else to do"
    end

    function Middle:method()
        print "Middle method, calling Base method..."
        -- call the version in Base
    end

    function Leaf:method()
        print "Leaf method, calling Middle method..."
        -- call the version in Middle
    end

The problem is to write the code for the two commments.

The "C++ solution" is to explicitly name the superclass in the call.

You seem not to want to do that. The problem is that when we call to
Middle:method the value of self is the same as it was in Leaf:method. So, an
expression only involving self doesn't have enough information to result in
a call to Base:method instead of another call to Middle:method. Remember
self._CLASS will always be Leaf.

Languages that provide "super" as a keyword take steps in the compiler to
give it meaning. For example, Objective-C performs super class calls via:

    id objc_msgSendSuper(
            struct objc_super* superContext,
            SEL theSelector,
            ...)

where objc_super is defined as "including the instance of the class that is
to receive the message and the superclass at which to start searching for
the method implementation".

Mark