Can we just have struct inheritence already?

Manu turkeyman at gmail.com
Sun Jun 9 21:13:50 UTC 2019


On Sun, Jun 9, 2019 at 6:59 AM Jonathan M Davis via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> 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,

I've been doing this for 10 years, and I'd rank it up there with 'ref'
in terms of annoying weird shit.

> 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.

Woah, that's a weird logical leap! Why would you or anyone expect a
struct to be polymorphic now? It's not heap allocated, and has no
vtable or virtual, or final, or any of those things.
You've gotta really reach to make that conclusion, and the compiler
will produce errors informing you every step of the way.

> 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.

Classic response.

Because:
1, that sucks!
2, DRY; some structs have common elements. (how can anyone find that
surprising?) I don't want to repeat them all over the place!
3, syntax highlighting breaks from that point down !!!
4, debuginfo breaks from that point down !!!
5, debugging experience is embarrassing !!!
6, it depends on naming conventions (of the base object/function),
which are brittle
7, meta to determine the base type is problematic and very complex if
it wants to be accurate
8, did i mention it just plain sucks!

This is NOT polymorphism, we're not talking about polymorphism, I wish
people would not change the topic.


More information about the Digitalmars-d mailing list