Suggestion: Walter Bright, why not to implement multiple inheritance in D?

Larry Evans cppljevans at cos-internet.com
Thu Dec 21 11:23:17 PST 2006


On 12/12/2006 09:49 PM, Walter Bright wrote:
 > Larry Evans wrote:
 >
 >> So...how would this be done in D?  Since the above application
 >> involved calculating grammar lookahead sets, and since you're
 >> interested in
 >> emulating spirit in D, this might provide some further motivation
 >> for finding a D equivalent :)
 >
 >
 > Could you provide a distilled explanation of what dynamic inheritance is?

:* Distilled_Explanation:

Dynamic Inheritance (a.k.a. DI, as coded here:

in http://tinyurl.com/yd7ean

) is really nothing more than the
"infrastructure" for implementing "Dynamic Dual Heirarchies" as
described on p. 5 of:

http://www.objectmentor.com/resources/articles/dih.pdf

which says:

   A dual heirarchy is like a ladder. The two inheritance heirarchies
   are the supports of the ladder, and in the INTELLIGENT CHILDREN
   pattern above, the 'has' relationships in the peers are the rungs of
   the ladder.

An "abstract" picture of such a dual heirarchy is in figure 3 on p. 4.
The left side of figure 3 is closer to DI than the right side.  This
is because the 'has' relationship is stored in the base class ( DI's
dyn_inherit::inherit<,,>::super_type::my_ref, corresponding to figure
3's right-pointing solid arrow from B1 to B2).  Thus, DI's inherit<,,>
corresponds to figure 3's B1 and DI's inherit's DynSupertype template
parameter corresponds to figure 3's B2.

A more "concrete" picture is figure 2, where lhs of figure 3's B1
corresponds to figure 2's Observer, and lhs of figure 3's B2
corresponds to figure 2's Subject.  In contrast to the Observer pattern
which figure 2 describes, DI just has one pointer (as opposed to a set
of pointers) from B2 (Subject) to B1 (Observers).  This single pointer
is stored in the dyn_inherit::base::my_subtype member of the DynSupertype of
inherit (note the REQUIREMENTS: comment after DynSupertype specifying
that it's derived from dyn_inherit::base).

:* Preliminary_Conclusion

Well, Walter, after I complete the :* Distilled_Explanation below, I
realized there's probably no need for Proxy<> supertype of
dyn_inherit::inherit,
Instead this class could be made a field and the "relevant" function
calls forwarded to the Proxy<>::get_ref() as done with the current
implementation (e.g. as done by the eff::exp_tree::top::begin method here:

http://tinyurl.com/yht62d

).  However, that still leaves the dyn_inherit::base and DeltaType
supertypes of dyn_inherit::inherit. So..., how would
dyn_inherit::inherit be coded in D?

;* Name_and_Location_Rationale:

Some of the names (for example DynSuperType and  DeltaType for the 1st
and 2nd inherit template arguments) may seem mysterious. The following
explains how these names were selected.

:** DynSuperType_Rationale:

The reason for the DynSuperType (and it's inclusion in the
Proxy<DynSuperType> supertype of inherit) is that type inherit
supertype is "supposed" to correspond to the *p supertype of C in:

   class C : *p { ... };

from section 12.7, "Delegation", of Stroustrup's
_Design & Evolution of C++_.  The reason for the "supposed" qualifier
above is that there's no automatic forwarding to *p as described in
section 12.7.  Since D has delegates, I thought maybe D could do this
forwarding ( I confess, I've not written a single D program yet ).
However, after reading:

   http://www.digitalmars.com/d/type.html#delegates

I guess not, since delegates point to single functions instead of to
classes :( .

Would Mixin's be any help?.  Well,
http://www.digitalmars.com/d/mixin.html seems to say that a select set
of declarations are *copied* from one context into another; however,
what I want needed is delegation to each of the selected functions in
the source context (i.e. the SynSuperType).  Hence, I guess mixins
wouldn't help either :(

:** DeltaType_Rationale:

The Delta comes from p. 2 of _Mixin-based Inheritance_ by Braca and
Cook, which is available here:

http://citeseer.ist.psu.edu/bracha90mixinbased.html

.  This Mixin part of the title tempts me to rethink whether D's
mixin's can be some help, but, as mentioned above, I've not yet
written a D program.  I guess I should start ;) .



More information about the Digitalmars-d mailing list