Why can't we derive struct's?

Manu turkeyman at gmail.com
Thu Dec 20 08:15:53 UTC 2018


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:
> > I've long since become bored of this design decision. Is there good
> > reason for it?
>
> `alias this` can alias to a function, that doesn't work very well with the base
> class syntax (you also use alias this to a function).

Right, by necessity, because an empty struct member takes padded space
in the aggregate.
I think C++ gets the empty-base-class mechanic right; it doesn't take space.

I'm demonstrating crappy code that appears extremely frequently in lieu.
Perhaps you have a better suggestion?

> > Why should it be impossible to derive a struct?
>
> Because polymorphism makes little sense for a value type.

It's not really polymorphism without a vtable, just simple extension.
`alias this` produces the same result, it's just more wordy, kinda
ugly, and boilerplate-ey. It also hogs the `alias this` slot.

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


More information about the Digitalmars-d mailing list