Why can't we derive struct's? (Hansel & Gretel)

luckoverthere luckoverthere at gmail.cm
Fri Dec 28 00:25:14 UTC 2018


On Thursday, 27 December 2018 at 20:15:04 UTC, Jonathan M Davis 
wrote:
> As it is, I think that having even a single alias this should 
> be done with caution and relatively rarely. And if we had the 
> ability to have multiple alias this-es on a type, I would think 
> that it should be used extremely rarely. But the very fact that 
> it existed could have some nasty repercussions (especially for 
> generic code). I don't know that that means that we shouldn't 
> have the feature, but it definitley concerns me.
>
> - Jonathan M Davis

Arguably, in a sense, we already have multiple alias this's. It 
just has to be done through 1 alias this per struct.


struct A {
   int a;
};

struct B {
     A a_;
     int b;
     alias a_ this;
}

struct C {
     B b_;
     int c;
     alias b_ this;
}

void main() {
     import std.stdio;

     C c;

     c.b = 10;
     c.a = 20;

     B b = c;
     A a = c;

     writeln( a, " ", b );
}



More information about the Digitalmars-d mailing list