Struct inheritance

Nick Treleaven nick at geany.org
Thu Sep 19 18:57:52 UTC 2024


On Thursday, 19 September 2024 at 15:56:06 UTC, Richard (Rikki) 
Andrew Cattermole 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 {
>     ...
> }
> ```

In the talk, Walter mentioned doing that instead of having `alias 
this`. That would be much less powerful. `alias this` allows 
hooking conversion to the target type. With `alias this = func; T 
func();`, you can create the T instance at runtime. It doesn't 
even have to be the same instance each time. Also T doesn't have 
to be a struct.

> 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;
> ```

This seems like `alias this` in disguise.


More information about the dip.ideas mailing list