Can we just have struct inheritence already?

Manu turkeyman at gmail.com
Mon Jun 10 18:27:33 UTC 2019


On Mon, Jun 10, 2019 at 2:40 AM Walter Bright via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> On 6/9/2019 10:52 PM, Manu wrote:
> > On Sun, Jun 9, 2019 at 9:50 PM Walter Bright via Digitalmars-d
> > <digitalmars-d at puremagic.com> wrote:
> >>
> >> On 6/9/2019 1:05 AM, Manu wrote:
> >>> I am really really tired of this pattern:
> >>
> >> Is your real complaint this:
> >> ---
> >>     #include <stdio.h>
> >>
> >>     struct B { };
> >>     struct S : B { char c; };
> >>     struct T { B b; char c; };
> >>
> >>     void main(){
> >>       printf("%d %d\n", sizeof(struct S), sizeof(struct T));
> >>     }
> >> ---
> >> which prints:
> >>
> >>     1 2
> >>
> >> ?
> >
> > That is indeed the feature that necessitates my 'solution', but `alias
> > this` is generally undesirable in this pattern in its own right, for
> > some of the reasons I listed.
>
> I don't see where you listed reasons? Other than it just being unsightly?

Somehow this thread forked from my OP, I listed reasons in my second
post there, mostly relating to editor and debug experience.
This pattern I illustrate has a tendency to obscure code and intent
not just to human readers, but also to the IDE tooling.
Syntax highlighting fails, auto-completion fails, debuginfo does not
understand the hack and presents incorrectly, etc.

> > `alias this` does not feel satisfying in this context in its own
> > right. Also, if this hack takes the alias this slot, then it can't be
> > used for what it's usually used for (enabling implicit conversion) and
> > sometimes needs conflict.
>
> Are you using 'alias this' to try to do multiple inheritance? Please don't :-(

No, I usually use alias this for implicit conversion... which is a
pattern I would be overjoyed to abandon if we had some way to mark
constructors or cast operators as implicit!
I also use alias this to fake struct inheritance.

I'd argue that I shouldn't be using alias this for either of these
reasons. They're both ugly hacks in the face of language deficiency
which really subvert the intent of the code, and ruin the editor/debug
experience.


More information about the Digitalmars-d mailing list