Can we just have struct inheritence already?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Jun 9 13:59:25 UTC 2019


On Sunday, June 9, 2019 2:05:34 AM MDT Manu via Digitalmars-d 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!

I've never even seen code use such a pattern before, and honestly, it seems
really weird to me to even write code that acts like one struct is derived
from another, because without polymorphism, I wouldn't really have thought
that that would make any sense.

However, if you're using this pattern frequently, what's stopping you from
just creating a function or template to use with mixin that takes care of it
for you? I would have thought that it would be fairly straightforward to do
something like

struct DerivedStruct
{
    mixin(aliasAsMember!BaseStruct);
}

and then it really isn't any more complex than

struct DerivedStruct : BaseStruct
{
}

would be.

- Jonathan M Davis





More information about the Digitalmars-d mailing list