structs inheriting from and implementing interfaces
Random D user
no at email.com
Fri Dec 29 14:29:52 UTC 2017
On Friday, 29 December 2017 at 12:03:59 UTC, Mike Franklin wrote:
> In C#, structs can inherit from and implement interfaces.
> Is that simply because it hasn't been implemented or suggested
> yet for D, or was there a deliberate design decision?
>
> Thanks for your insight,
>
> Mike
I think it's deliberate, structs are just plain dumb value types.
If I remember correctly I think Remedy's Binderoo C++ bindings
implemented C++ inheritance on top of structs. You might want to
look at that.
Or you could do C-style "inheritance" and slap some D magic on
top of that.
Some pseudo code:
struct Base
{
enum SubType subtype;
int someBaseField;
}
struct Child1
{
Base base; // Must be first
int foo;
}
struct Child2
{
Base base;
float bar;
}
Base b;
Child1 c1;
Child2 c2;
base_doSomething(Base* b);
child1_doSomething(Child1* c1);
child2_doSomething(Child2* c2);
base_doSomething(cast(Base*)&c1);
switch(base.subtype)
{
case Child1:
child1_doSomething(cast(Child1*)&b); break;
case Child2:
child2_doSomething(cast(Child2*)&b); break;
}
// add some alias this and other d things to smooth things out.
More information about the Digitalmars-d-learn
mailing list