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

Burton Radons burton-radons at smocky.com
Sat Dec 2 20:11:39 PST 2006


Larry Evans wrote:
> On 12/02/2006 09:27 AM, Burton Radons wrote:
> [snip]
>> I don't have a perfect solution to the problem; there is none because 
>> it's asking two objects to exist in one space and we need to resolve 
>> that. But given that multiple inheritance is sometimes necessary, 
>> waving it off as "too complex" or "bad form" is impractical; like any 
>> type of suppression, that only leads to cracking and nasty leaks 
>> (mixins).
>>
>> I have an idea. Let's try making this assertion: no type will have in 
>> its inheritance tree another type multiple times without being an 
>> abuse of multiple inheritance. Given this statement, can anyone find an 
> [snip]
>> Yeah, but C++ programmers are programming in C++. The hell do they 
>> know about good language practices? ;-)
> 
> How would the D equivalent to mpl::inherit_linearly<, inherit<,>, >
> (see http://www.boost.org/libs/mpl/doc/refmanual/inherit-linearly.html)
> work without using the MI class, inherit?

That's a good example. Here's another from Wikipedia:

     Animal
     Mammal : Animal
     Winged : Animal
     Bat : Mammal, Winged

Contrived but not unreasonable, but I think the problem I gave applies 
even if they don't override any of the parent methods, just as a pure 
maintainer problem of the subclasses depending upon the implementation 
of the parent class.

We could restrict diamonds to interfaces, like it's done now, but that 
feels like a hack and doesn't actually solve the problem - in fact, it 
makes the problem necessary. Damn. I retract my challenge.

Perhaps we're trying to shoehorn two types of inheritance into one tree. 
Something that's Winged is an Animal, but it's not a /complete/ animal; 
it's not going to have any opinion on its feeding habits. So we could use:

     Animal
     Mammal : Animal
     Winged : abstract Animal
     Bat : Mammal, Winged

And allow the diamond because Winged can't override anything from 
Animal. This is bad because Animal might have stuff Winged has an 
opinion on, but more discrete selections of inheritance (which are a 
good idea anyway) allows us to defeat the diamond when we need to:

     Reproduction
     Movement
     Animal : Reproduction, Movement
     Mammal : abstract Animal, Reproduction
     Winged : abstract Animal, Movement
     Bat : Mammal, Winged

In this case Mammal and Winged are exclusive, so the tree works properly.

I'd analyse your example more but I'm unfamiliar with Boost. Can you 
factor the problem so that it can be expressed in terms of animals? :-)



More information about the Digitalmars-d mailing list