View mode: basic / threaded / horizontal-split · Log in · Help
March 11, 2008
Struct Inheritance (not concept interfaces)
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.
March 11, 2008
Re: Struct Inheritance (not concept interfaces)
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
March 11, 2008
Re: Struct Inheritance (oopsy alias this)
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.
March 11, 2008
Re: Struct Inheritance (oopsy alias this)
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.
March 11, 2008
Re: Struct Inheritance (not concept interfaces)
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.
Top | Discussion index | About this forum | D home