Can we just have struct inheritence already?

Nick Sabalausky (Abscissa) SeeWebsiteToContactMe at semitwist.com
Sun Jun 9 19:04:50 UTC 2019


On 6/9/19 4:05 AM, 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!
> 

Not necessarily objecting here, but does this pattern really come up all 
that often?

I'm not even entirely sure what's going on in the "static else" there. 
Is it just saying:

"If the BaseStruct is all methods and no data, then don't bother letting 
`BaseStruct base` add extra obviously-never-used bytes in DerivedStruct?"

If so, it sounds like maybe that's an optimization the language/compiler 
could/should be doing (semi-?)automatically?


More information about the Digitalmars-d mailing list