classes structs

Piotr Szturmaj bncrbme at jadamspam.pl
Fri Sep 14 16:30:23 PDT 2012


David Currie wrote:
> At the risk of appearing ignorant, I don't know everything about D.
> However in D I have noticed the following.
>
> It is a policy decision in D that a class is ALWAYS on the heap and passed
> by REFERENCE. (I know there is a keyword to put a class object on the stack
> but this is not facile and needing a workaround is poor language design).
> This is effectively FORCED Java.
> A D struct is on the stack and is NOT a class and has NO inheritance.
>
> I have issues with this philosophy.

I'm not comfortable with it either. But as you know, there is 
scoped!MyClass library solution. One thing I miss is a Scoped!MyClass 
struct so I could embed my classes into structs without using heap. Then 
a construct() function could be used on it to init members and to call 
constructors (emplace() may be used to do that).

> ALL struct VARIABLES when declared are initialised to their .init value.
> Just in case a programmer "forgets" to initialize them.
> This is like using a sledgehammer instead of a scalpel.
> Could you answer me WHY??

For safety and for easier generic programming. You can always disable 
automatic initialization by using void initializer.

> ALL classes when declared are instantiated on the heap
> and their constructor called. Again I ask WHY??

This is wrong. Classes when declared are initialized to null (unless 
void initializer is used). They're not automatically instantiated. You 
must explicitly instantiate them using _new_ operator.


More information about the Digitalmars-d mailing list