Why can't structs be derived from?

Piotr Szturmaj bncrbme at jadamspam.pl
Wed Mar 16 08:52:56 PDT 2011


Ary Manzana wrote:
> Syntax matters. A lot. Which one is more readable/understandable?
>
> struct Point2 {
> int x;
> int y;
> }
>
> 1.
>
> struct Point3 {
> Point2 point2;
> alias this point2;
> int z;
> }
>
> 2.
>
> struct Point3 : Point2 {
> int z;
> }
>
> You can't deny this last one is much more easier to understand and it
> exactly does what your mind want to do: just give me what's in the other
> struct and let me add more things.
>
> The compiler can implement this using alias this and making the aliased
> member private, and possibly disallowing adding another alias this.

Alias this gives you more power in case of struct serialization. You can 
place new fields before or after "inherited" struct, or even on both 
sides. In your 2nd case, z will be always after x and y.


More information about the Digitalmars-d mailing list