digitalmars.D - Simulating Multiple Inheritance
- dsimcha (25/25) Oct 28 2010 I've just found a new D-specific design pattern for simulating non-virtu...
- Simen kjaeraas (4/22) Oct 28 2010 I believe this is mentioned in TDPL.
- Jimmy Cao (2/2) Oct 28 2010 Well, you can't have multiple alias this's right?
- Simen kjaeraas (23/25) Oct 28 2010 It is extensible like this:
- Simen kjaeraas (5/6) Oct 28 2010 Problem is of course, no reliable way to hijack
- Jesse Phillips (2/8) Oct 28 2010 This is an issue with the implementation, the language allows for as man...
- Jonathan M Davis (5/15) Oct 28 2010 Yeah. TDPL specifically says that you can have multiple alias this's (in...
- tls (2/17) Oct 29 2010 So no D2 staple for me ? When is ready? TDLP not high priority?
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
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
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
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
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
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
On Thursday, October 28, 2010 15:46:23 Jesse Phillips wrote:Jimmy Cao Wrote: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 DavisWell, 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
Jonathan M Davis Wrote:On Thursday, October 28, 2010 15:46:23 Jesse Phillips wrote:So no D2 staple for me ? When is ready? TDLP not high priority?Jimmy Cao Wrote: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.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 29 2010