Struct inheritance

Ogion ogion.art at gmail.com
Mon Sep 23 14:52:20 UTC 2024


On Thursday, 19 September 2024 at 15:56:06 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
>>

Would be nice to have proper struct inheritance but there are a 
lot of things we have to consider.

What about slicing?

```D
struct A {
     int x;
}

struct B : A {
     int y;
     this(int x, int y) {
         super(x);
         this.y = y;
     }
}

void main() {
     B b1 = B(1, 2), b2 = B(3, 4);
     A* p = &b2;
     *p = b1;
     writeln(b2); // B(1, 4)
}
```

If we want something better than `alias this`, we have to 
prohibit implicit partial assignment via pointer/reference. When 
it’s necessary, it would still be possible with `*p = cast(A)b1;`.



More information about the dip.ideas mailing list