Can we just have struct inheritence already?

Walter Bright newshound2 at digitalmars.com
Tue Jun 18 19:58:22 UTC 2019


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 :-)


More information about the Digitalmars-d mailing list