Can we just have struct inheritence already?

Timon Gehr timon.gehr at gmx.ch
Wed Jun 19 00:01:03 UTC 2019


On 18.06.19 21:58, Walter Bright wrote:
> On 6/18/2019 3:34 AM, Ethan wrote:
>>> Have you considered using template mixins? Since you're using a base 
>>> struct merely as a namespace for some functions, that should work.
>>
>> The ABI compatibility point really needs to be reiterated here. If the 
>> end goal, especially with things like D++, is to drive adoption of D 
>> by ensuring C++ interop is simple then that's now going to result in a 
>> static analysis tool to look at any given C++ struct and work out 
>> whether it should or shouldn't be converted to a mixin template. And 
>> introduce the dissonance of a C++ type not actually being a type in D.
> 
> You can do this:
> 
>    mixin template BaseMembers() {
>      void memberFunction() { ... }
>    }
> 
>    struct Base {
>      mixin BaseMembers!();
>    }
> 
>    struct Derived {
>      mixin BaseMembers!();  // walla! zero size for "inheritance"!
>      Base b;            // when I want C++ 1 byte size
>    }
> 
> It's not a thing of beauty, but nothing connected with C++ is :-)

This works if Base and Derived are defined in the same module. Otherwise 
this does not do the correct thing, because mixin templates are analyzed 
in the scope into which they are mixed.


More information about the Digitalmars-d mailing list