Why can't we derive struct's?

Manu turkeyman at gmail.com
Thu Dec 20 18:20:54 UTC 2018


On Thu, Dec 20, 2018 at 7:00 AM Steven Schveighoffer via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On 12/20/18 3:15 AM, Manu wrote:
> > On Wed, Dec 19, 2018 at 7:45 PM Walter Bright via Digitalmars-d
> > <digitalmars-d at puremagic.com> wrote:
> >>
> >> On 12/19/2018 5:40 PM, Manu wrote:
> >>   > static if (Base.tupleof.length == 0)
> >>
> >> static if (Base.sizeof == 0)
> >
> > Empty struct has sizeof == 1 no? It certainly takes that many bytes as
> > a member, and that 1 byte is naturally padded out to the alignment of
> > the following member.
>
> Yes, it does take 1 byte. I believe the reasoning was so it could have a
> unique address and be used in an array.
>
> It's the same in C++ as well, according to the Internet (except for the
> base class optimization allowance). Note that C++ compilers aren't
> *required* to implement this.
>
> The difference between inheritance and manual inheritance (i.e. alias
> this and a member), is that with true inheritance there is no member
> that has to have it's own distinct address, so it's allowed to take up 0
> space
>
> But I would expect your boilerplate could be handled via mixin template
> pretty easily, then it becomes:
>
> struct Derived
> {
>     mixin DeriveFrom!Base;
> }

Right, but that's still pretty shit, and I still don't know what
advantage it has? Now the pattern you have to look for to understand
you are dealing with a derived type is even more bespoke; every code
base will have a different expression for this, and people might do it
differently in different cases.
Every single person I've interacted with that's encountered D has been
surprised by this. I'm not sure there's a good reason for it.

I'm not saying the current method shouldn't exist, or that it's bad
design or not useful. I'm just saying that BOTH should exist, and the
syntax may just be sugar for simple, common, and expected aggregation.
It can even call the member 'super', instead of some random name.
`alias this` is great, but it's too manual, too much noise, and not
searchable, which isn't great for the simple case which is deployed
99% of the time.

> That it takes up the one alias this slot is a bummer, but really we
> should implement multiple-alias-this and that problem would be solved.

That's been accepted for years now... why is there no movement? Is the
features as defined incompatible with the implementation of DMD?


More information about the Digitalmars-d mailing list