www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Simulating Multiple Inheritance

reply dsimcha <dsimcha yahoo.com> writes:
I've just found a new D-specific design pattern for simulating non-virtual
multiple inheritance by abusing inner classes and alias this.  Here's an
example:

class Foo {
    uint num = 2;
}

class Bar {
    uint num = 3;
}

class Outer : Foo {

    class Inner : Bar {}

    Inner inner;

    this() {
        inner = new Inner;
    }

    alias inner this;
}

Basically, this satisfies all of the requirements for simulated multiple
inheritance:

1.  An Outer can be implicitly converted to either a Foo or a Bar.

2.  It obtains both interface and implementation from both Foo and Bar.

3.  The methods of Outer that override stuff from Foo have access to
Bar-related stuff and vice-versa.

Should this be listed somewhere as D's answer to lack of multiple inheritance?
 I find it very cool that the language is expressive enough to reasonably
simulate MI.
Oct 28 2010
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
dsimcha <dsimcha yahoo.com> wrote:

 I've just found a new D-specific design pattern for simulating  
 non-virtual
 multiple inheritance by abusing inner classes and alias this.  Here's an  
 example:

 class Foo {
     uint num = 2;
 }

 class Bar {
     uint num = 3;
 }

 class Outer : Foo {

     class Inner : Bar {}

     Inner inner;

     this() {
         inner = new Inner;
     }

     alias inner this;
 }
I believe this is mentioned in TDPL. -- Simen
Oct 28 2010
prev sibling parent reply Jimmy Cao <jcao219 gmail.com> writes:
Well, you can't have multiple alias this's right?

So then, wouldn't it still only limit to 2 base classes?
Oct 28 2010
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Jimmy Cao <jcao219 gmail.com> wrote:

 Well, you can't have multiple alias this's right?

 So then, wouldn't it still only limit to 2 base classes?
It is extensible like this: class Outer : Foo { class Inner : Bar { class InnerInner : Baz { } InnerInner innerInner; this() { innerInner = new InnerInner; } alias innerInner this; } Inner inner; this() { inner = new Inner; } alias inner this; } It should be possible to turn this into a mixin. -- Simen
Oct 28 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Simen kjaeraas <simen.kjaras gmail.com> wrote:

 It should be possible to turn this into a mixin.
Problem is of course, no reliable way to hijack constructors to initialize the inner class. -- Simen
Oct 28 2010
prev sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
Jimmy Cao Wrote:

 Well, you can't have multiple alias this's right?
 
 So then, wouldn't it still only limit to 2 base classes?
 
 Well, you can't have multiple alias this's right?<div><br></div><div>So then,
wouldn't it still only limit to 2 base classes?</div>
 
This is an issue with the implementation, the language allows for as many alias this as you wish.
Oct 28 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, October 28, 2010 15:46:23 Jesse Phillips wrote:
 Jimmy Cao Wrote:
 Well, you can't have multiple alias this's right?
 
 So then, wouldn't it still only limit to 2 base classes?
 
 Well, you can't have multiple alias this's right?<div><br></div><div>So
 then, wouldn't it still only limit to 2 base classes?</div>
This is an issue with the implementation, the language allows for as many alias this as you wish.
Yeah. TDPL specifically says that you can have multiple alias this's (in the section on this exact topic too, IIRC), so it's on the list of things that TDPL says and D that dmd hasn't correctly implemented yet. - Jonathan M Davis
Oct 28 2010
parent tls <do notha.ev> writes:
Jonathan M Davis Wrote:

 On Thursday, October 28, 2010 15:46:23 Jesse Phillips wrote:
 Jimmy Cao Wrote:
 Well, you can't have multiple alias this's right?
 
 So then, wouldn't it still only limit to 2 base classes?
 
 Well, you can't have multiple alias this's right?<div><br></div><div>So
 then, wouldn't it still only limit to 2 base classes?</div>
This is an issue with the implementation, the language allows for as many alias this as you wish.
Yeah. TDPL specifically says that you can have multiple alias this's (in the section on this exact topic too, IIRC), so it's on the list of things that TDPL says and D that dmd hasn't correctly implemented yet.
So no D2 staple for me ? When is ready? TDLP not high priority?
Oct 29 2010