Multiple subtyping with alias this and nested classes

Leandro Lucarella llucax at gmail.com
Fri Oct 2 15:35:55 PDT 2009


Andrei Alexandrescu, el  2 de octubre a las 13:00 me escribiste:
> I just realized that nested classes work so well with alias this,
> you'd think it was an evil plot all along. It wasn't, but I'm happy
> about the coincidence.
> 
> Here's how to effect multiple subtyping in D very effectively:
> 
> import std.stdio;
> 
> class Base1 {
>     void fun() { writeln("Base.fun"); }
> }
> 
> class Base2 {
>     void gun() { writeln("Base.fun"); }
> }
> 
> class Multiple : Base1 {
>     // Override method in Base1
>     override void fun() { writeln("Multiple.fun"); }
>     // Override method in Base2
>     class MyBase2 : Base2 {
>         override void gun() { writeln("Multiple.gun"); }
>     }
>     // Effect multiple subtyping
>     Base2 _base2;
>     alias _base2 this;
>     this() {
>         _base2 = new MyBase2;
>     }
> }
> 
> void main()
> {
>     auto obj = new Multiple;
>     Base1 obj1 = obj;
>     obj1.fun();
>     Base2 obj2 = obj;
>     obj2.gun();
> }
> 
> The program above segfaults because somehow obj2 is null. That is a
> bug I just reported. For now, you can replace obj2.gun() with
> obj.gun() to make things work.
> 
> When we first introduced alias this, I knew multiple subtyping was
> possible. I didn't expect it to dovetail so nicely with nested
> classes.

We might have very different taste, but I find that a little... horrible.
What do you have against mixins? I think you're trying to use D as C++ :)

-- 
Leandro Lucarella (AKA luca)                      http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
EL PRIMER MONITO DEL MILENIO...
	-- Crónica TV



More information about the Digitalmars-d mailing list