www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Struct Inheritance (not concept interfaces)

reply Sclytrack <sclytrack pi.be> writes:
With struct inheritance I mean the following and not the "concept interfaces".

struct Base
{
  void doStuff()
  {
  }
}

struct Derived
{
  inherit Base base;
}


  Derived d;
  d.doStuff();


Sort of like having a compile time inheritance, with its limitations.
Mar 11 2008
next sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Sclytrack schrieb:
 Sort of like having a compile time inheritance, with its limitations.

See also this thread http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=64764
Mar 11 2008
parent reply Sclytrack <Sclytrack pi.be> writes:
Forget what I said, I see that there is already something specified in the
"WalterAndrei.pdf". Something called "alias this" and I forgot about it.

"allows importing the fields of a member into the namespacece of a struct."

	struct M {int a; }
	struct S {
		M m;
		alias m this;
		int b;
	}

	S s;
	b.a;
	s.b;

Wonder if it works with member functions too.
Mar 11 2008
parent "Koroskin Denis" <2korden gmail.com> writes:
On Tue, 11 Mar 2008 14:02:26 +0300, Sclytrack <Sclytrack pi.be> wrote:

 Forget what I said, I see that there is already something specified in  
 the
 "WalterAndrei.pdf". Something called "alias this" and I forgot about it.

 "allows importing the fields of a member into the namespacece of a  
 struct."

 	struct M {int a; }
 	struct S {
 		M m;
 		alias m this;
 		int b;
 	}

 	S s;
 	b.a;
 	s.b;

 Wonder if it works with member functions too.

As of now, it's not implemented. Your best best whould be to use mixins, and it's almost 100% same syntax: template Base() { void doStuff() { } } struct Derived { mixin Base; } as opposed to: struct Base { void doStuff() { } } struct Derived { inherit Base base; } However, you still can't cast Derived to Base. And proposed syntax (one with aliased this) most probably won't too (at least, S doesn't look like it inherits M). I still think that structs should just support inheritance, maintaining the limitation of not having virtual functions. Classes could also benefit from deriving from structs, although this most probably won't ever happen, since classes are reference types and most importantly contain _vtptr as a first member and therefore no casting is possible between them.
Mar 11 2008
prev sibling parent Christopher Wright <dhasenan gmail.com> writes:
Sclytrack wrote:
 With struct inheritance I mean the following and not the "concept interfaces".

If you're going to have any syntax for it, why not use the same syntax that you get with classes? You'd have no polymorphism; the only benefits would be transparent aggregation and maybe `is (DerivedStruct : BaseStruct)` would return true. However, you can get automatic aggregation with some (perhaps slightly tedious) template magic. I'm not interested in writing it, though, because it's tedious and fulfills no need for me.
Mar 11 2008