Thursday, September 07, 2006

Multiple Inheritance in Mynx Part Deux

Multiple inheritance is fraught with peril, how does Mynx handle the features of multiple inheritance?

Simple - by not dealing with them. Huh? Or to paraphrase an old maxim:

"If you open a can of worms, you'll need a bigger can." To avoid the problem do not create it...Mynx might use the infamous can of worms, but it does not open it.

Multiple inheritance in C++ requires either dis-ambiguation to avoid a vague reference in a super-class, or specifying a class as virtual to avoid repeated inheritance. Eiffel allows specifying which super-class is selected.

The overall problem is ambiguity, with two classes with overlapping methods, or with overlappig super-classes, a sub-class either has ambiguity or repeated inheritance. So Mynx insists that inheritance as multiple is disjoint (a term borrowed from set theory) so that super-classes do not overlap in method names/signatures, or in super-classes. Hence the term, multiple-disjoint inheritance.

For example, repeated inheritance:

class A as null is
     public construct is to null;
     public void doh is to null;
end class;

class B as A is
     public construct is to null;
     public void duh is to null;
end class;

class C as A is
     public construct is to null;
     public void dah is to null;
end class;

class D as A,B is
     public construct is to null;
     public void deh is to null;
end class;


Class D is multiply disjoint from classes B and C, but both classes B, C are
sub-classes of A -- repeated inheritance. Illustrating using arrow notation:


A->0 (use zero for null super-class)

B->A

C->A

D->B->A
D->C->A


Now the problem is very plain to see--class D has repeated inheritance of class
A through the super-classes B, C.

The problem would be noted as an external semantic error of repeated inheritance
of a common ancestor super-class A. The information about a class, and its
super-classes is stored in a MOXI file, and XML file that contains information
about a class--including all super-classes, including the class's super-classes.

To work in Mynx, the classes must be rewritten as:

class A as null is
     public construct is to null;
     public void doh is to null;
end class;

class B as null is
     public construct is to null;
     public void duh is to null;
end class;

class C as null is
     public construct is to null;
     public void dah is to null;
end class;

class D as A,B,C is
     public construct is to null;
     public void deh is to null;
end class;


Now the multiple inheritance is truely disjoint, no problems with repeated inheritance, but the classes used must be designed intentionally with the goal of multiple inheritance in mind.

Using arrow notation:

A->0 (use zero for null super-class)

B->0

C->0

D->A->0
D->B->0
D->C->0


The common super-class is 0 or null -- which is not an actual super-class (although for consistency it is considered a class).

In the triumvirate of C++, Java, and C# one of the features of C++ that I really like (and didn't really realize...one of the benefits of designing your own programming language, you find features in existing languages you like) is that C++ does not have an implicit hierarchy of classes.

Both Java and C# both have "Object" as the default, defacto super-class. With C++ there is the option of using a class hierarchy...in terms of more powerful or flexible, C++ you do not have to use a class hierarchy, but in both Java and C# you cannot unuse a super-class (you can with an interface...but not all classes...). Thus the C++ flexibility of choice for a super-class object is a powerful feature.

Mynx uses null inheritance like C++'s flexibility of not using a super-class. The flexibility is necessary for multiple-disjoint inheritance. Forcing a super-class of "Object" or "superObject" class will always create both repeated inheritance and name ambiguity.

So a very little recognized feature of C++ is in fact a very powerful feature for multiple-inheritance. Mynx borrows this from C++, and the notion that if a software developer does not want to use a base super-class, it is up to them...particularly if the intent is a class for use in multiple inheritance.

C# is much like C++ (and C# is not a simple Java-clone as some Microsoft detractors have asserted in my humble opinion...) but C# does miss the boat in that the ability to choose a super-class or not. (On the other hand, C# does allow for delegation which is useful for achieving multiple inheritance like features...). Of course, both Java and C# have avoid the can of worms by throwing away the can.

Labels: , ,

Website Spy Software