Can we just have struct inheritence already?

Exil Exil at gmall.com
Sun Jun 9 14:45:47 UTC 2019


On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:
> I am really really tired of this pattern:
>
> struct DerivedStruct
> {
>     static if (BaseStruct.tupleof.length > 0)
>         BaseStruct base;
>     else
>         ref inout(BaseStruct) base() inout { return
> *cast(inout(BaseStruct)*)&this; }
>     alias base this;
>
>     // derived members
>     //...
> }
>
> Imagine if we could just write:
>
> struct DerivedStruct : BaseStruct
> {
>     // derived members
>     //...
> }
>
> Just imagine!

You can just put the boiler plate code in a mixin template.

mixin template  BaseAs(T) {
     static if(T.tupleof.length > 0) T base;
     else                            ref inout(T) base() inout { 
return *cast(inout(T)*)&this; }

     alias base this;
}

struct DerivedStruct {
     mixin BaseAs!BaseStruct;
}



More information about the Digitalmars-d mailing list