lua-users home
lua-l archive

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


on 1/22/05 2:02 PM, PA at petite.abeille@gmail.com wrote:

> Assuming an existing "description" method in the superclass one would
> like to extend, one could now invoke it like this:
> 
>    function MySubclass:description ()
>      super:description()
> 
>      print "my very own description as well"
>    end
> 
> Does this make any kind of sense?!?

Only if super is a local variable equal to the superclass. That's basically
the C++ solution -- i.e., explicitly specify the superclass in the upcall.

For example:

    MySubclass = class( MyBaseClass )
    local super = MyBaseClass

Now, I can feel free to write methods in MySubclass that reference super and
hence reference MyBaseClass provided that I write them in a context where
this definition of super is visible.

Of course, I could also just write them to explicitly mention MyBaseClass.
That becomes a maintenance issue, however, if I change the class hierarchy.

(I can do this in C++ as well by putting a typedef for super at the top of
each class.)

My point was that the value of self was insufficient on its own to determine
the point in the class chain to start looking when making a call to the
inherited version of a method because self always starts at the end of the
chain and we would need to know how far we'd gotten in recursing through the
calls to inherited versions of the method.

Mark

P.S. Lua environment skankery (not actually recommended): Classes could
define a custom environment that they assign to each of their methods that
would include a definition of super.