Struct inheritance
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Thu Dec 5 22:41:25 UTC 2024
On Thursday, September 19, 2024 9:56:06 AM MST Richard Andrew Cattermole
(Rikki) via dip.ideas wrote:
> As an idea this has come up in Razvan's DConf 2024 talk.
>
> 1. Support inheritance on struct, for other structs.
>
> ```d
> struct Parent {
> ...
> }
>
> struct Child : Parent {
> ...
> }
> ```
>
> 2. ``opDispatch`` function, may work in place of ``alias this``
> when no parent exists.
>
> ```d
> struct Parent {
> T thing;
> ref T opDispatch(string:"")() {
> return this.thing;
> }
> }
>
> struct Child : Parent {
> }
>
> Child child;
> T got = child;
> ```
After that talk, I discussed struct inheritance briefly with Walter, and his
idea was that if we did it, there would be _no_ conversions of any kind that
came from it. It would purely be a way to have a struct inherit all of the
fields and member functions from the "parent" struct and as such would
essentially be the compiler copying and pasting that code into the struct
doing the inheriting.
So, it would provide a way to inherit / copy the implementation, but the
types would be completely divorced from one another in terms of how they
were used.
Personally, I think that that's the right approach, since it's the issues
around implicit conversions that are the core problem with alias this, and
it avoids all issues with regards to slicing objects (which is the primary
reason why classes in D are on the heap instead of on the stack like they
can be in C++).
- Jonathan M Davis
More information about the dip.ideas
mailing list