Multiple subtyping with alias this and nested classes

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Oct 2 11:37:33 PDT 2009


language_fan wrote:
> Fri, 02 Oct 2009 13:00:05 -0500, Andrei Alexandrescu thusly wrote:
> 
>> 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 void fun() { writeln("Multiple.fun"); } 
>>      class MyBase2 : Base2 {
>>          override void gun() { writeln("Multiple.gun"); }
>>      }
>>      // Effect multiple subtyping
>>      Base2 _base2;
>>      alias _base2 this;
>>      this() {
>>          _base2 = new MyBase2;
>>      }
>> }
> 
> I do not get why didn't you just use the 'import' keyword here instead of 
> alias, since what you are basically doing is importing the symbols from 
> one scope to another. It would seem a perfect generalization of import to 
> also include cases where 'with' is currently used and also alias. The use 
> of the term 'alias' is somewhat confusing.

Well if we did use 'import' people would have asked why we don't use 
'alias' because that's much closer to what really happens.

Note that the feature does not only import symbols from one scope to 
another. It just allows you to substitute some expression when someone 
tries to use this under another type.


Andrei



More information about the Digitalmars-d mailing list