Difference between stack-allocated class and struct

Jonathan M Davis jmdavisProg at gmx.com
Mon May 2 15:30:46 PDT 2011


> Firstly, thanks for comprehensive answer and I'd like to excuse for my
> stupid mistakes, which are caused by learning a lot and not actually
> programming.
> 
> On date 2011-05-02 23:03, Jonathan M Davis wrote:
> > Classes are reference types and are meant to be on the heap.
> 
> Yeah, value vs reference semantics - that was the thing i forgot...
> 
> > So, if you're using a scoped class, you're _not_ getting the benefits of
> > polymorphism.
> 
> I completely didn't think about, that stack size needs to be known
> already at time of compiling. You just probably saved me a lot of problems.
> 
> 
> So, scoped classes can't be used for filling gap in struct inheritance.
> 
> I'll clarify myself:
> All i would need is extending - without polymorphism.
> Containment, can be solution for fields which doesn't annoys so much
> (although image in auto-generated documentation, just like it's with
> subclassing, would be nice).
> Unfortunately, the worse case is about methods, which have to be
> manually forwarded to contained struct.
> 
> So, does someone sees any nice solution for method forwarding as
> described? Should i make use of some mixin's?

alias this is supposed to be one of the better solutions for dealing with the 
sort of problem that you're looking at. Unfortunately, it's highly buggy at 
the moment, so it's questionable as to whether it would work. And even if it 
works, you can currently only have one alias this per type, so if you need 
more than that, you'd need another solution (eventually, alias this should 
work just fine, and you should be able to have multiple of them per type, but 
not yet).

At the moment, you pretty much need to do manual forwarding. Now, with the 
clever use of compile-time reflection via __traits and/or std.traits along 
with template mixins or string mixins, you should be able to get the compiler 
to generate all of the forwarded functions for you, but you'd still have to 
write the code for generating the mixins, which could be somewhat tricky. If 
you get it right though, then it would generate all of the appropriate 
functions regardless of whether any are added to or removed from the type that 
you're forwarding function calls to.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list