[E-Lang] Re: inheritance, delegation, mental models for
beginners (was: down with `define' (was: newbie syntax: picayune
points from a prejudiced programmer))
Mark S. Miller
markm@caplet.com
Fri, 02 Mar 2001 16:31:55 -0800
At 12:06 PM Friday 3/2/01, zooko@zooko.com wrote:
> MarcS wrote:
>> [...] With delegation, a delegated-to class call to
>> the overridden method is directed to the delegated-to class implementation.
>> So I like your theory, I yearn to accept it, but I can't buy it :-)
>
>To me this is still inheritance. The salient difference between
>inheritance and delegation to me is:
>
>In inheritance, the "parent object" is (normally) one-to-one with the
>child object. I.e. if there is an existing object with its own state
>and I "inherit" from it, then I want a copy of the state (optionally a
>clean slate), rather than a link to shared state.
I think this paragraph confuses levels, but I don't know how to repair it
without destroying your carefully guarded naivete. When you're ready to
sacrifice this naivete, please read
http://www.erights.org/elang/blocks/inheritance.html and we'll revisit this.
>There are many other very interesting issues about this, including
>subtyping relations, class-based vs. object-based and dynamic vs.
>static, and the behaviour of recursive method invocations (which are,
>in fact, a facet of the perennial conundrum of covariance), but none of
>these, and least of all the last one, should preclude the name
>"inheritance".
I think exactly the last one should, as that's the semantic behavioral
difference.
>Heck, in C++ "inheritance" the default behaviour (without the "virtual"
>keyword) was neither the superclass nor the subclass, but the class
>which corresponded to the static type of the pointer! :-)
If C++ had no virtual keyword, then to my mind it would no longer have
inheritance, only the class-based equivalent of delegation.
>And C++ is by far the most widely used O-O language according to
>sourceforge.
And it is exceedingly rare for a C++ program to intentionally override a
non-virtual method from a superclass.
>I still think it is fair to say that E has inheritance, and then to
>explain to those 2% of the population who care, that is more like
>Smalltalk's "delegation" than Java's "inheritance", and it is a little
>like C++'s "inheritance (without the virtual keyword)", but better.
It doesn't correspond to anything directly supported or practiced by
Smalltalk programmers, although you can almost do delegate/match with a
doesNotUnderstand method. Smalltalk's inheritance corresponds exactly to
Java's or to C++'s with mandatory "virtual".
When C++ programmers leave out "virtual", it isn't because they want the
semantics of delegation, it's because they know they aren't overriding yet,
and they'd rather have the efficiency gain at the price of maintenance
brittleness.
>But I'm curious about this: is this behaviour (superclass methods
>invoking other superclass methods instead of the subclass's override)
>considered to be an advantage semantically or just easier to implement?
It's semantically much cleaner, and much easier to understand from an
instance-oriented perspective.
>I think I would prefer that the superclass invoke the subclass's
>method.
Then I'd say you prefer inheritance over delegation. But which Zooko is
saying this? I'd like to pierce the veil of naivete on this one. If it's
the naive Zooko, I'll congratulate you on simulating correctly. If it's the
real Zooko, I want to rewire your preferences. (Except for rare cases like
the CopyVisitor presented earlier.)
>When I said that I preferred delegation over inheritance I simply meant
>that I didn't want code re-use to imply subtyping.
Subtyping is a distinct concept that thankfully even already has its own
distinct name. ;) Java's "implements" is clearly subtyping without
inheritance or delegation.
Cheers,
--MarkM