www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixing struct and class subtypes with alias this and inheritance

You can define a struct subtype hierarchy by adding alias this 
declarations to each struct. Like this:

     S3 <: S2 <: S1

     struct S3 { auto toS2() { return S2(); } alias toS2 this; }
     etc.

You can also define a class subtype hierarchy by using class 
inheritance:

     C3 <: C2 <: C1

     class C3 : C2 {}
     etc.

But apparently the mechanisms don't mix. This works:

     C1 <: S3 <: S2 <: S1

     class C1 { auto toS3() { return S3(); } alias toS3 this; }

But this doesn't work:

     C2 <: C1 <: S3 <: S2 <: S1

     class C2 : C1 {}

I think that should work.

I know I can put an alias this in C2. But it seems like 
inheritance should be enough to bridge the gap, since subtyping 
should be transitive.
Jul 12 2018