structs vs classes

Jim bitcirkel at yahoo.com
Sat Jan 29 07:11:41 PST 2011


biozic Wrote:

> Le 29/01/11 14:43, Jim a écrit :
> > I'm a bit troubled with the class/struct dichotomy. I would prefer them both to use the same keyword. Heap/stack allocation could be specified during instantiation instead. Why? Now you need to choose one over the other. Not even C++ has this limitation.
> >
> > Think about containers for example, should they be classes or structs? Do you want them on the stack or on the heap?
> >
> > I guess it's possible to define the entire container as a mixin now. That would let you have both heap and stack containers share definition, but generally I think that the dichotomy should be abolished.
> 
> The difference between class and struct in D is more than heap or stack 
> allocation. Having a common keyword for them would unwisely mask their 
> fundamental differences (inheritance/polymorphism, reference/value 
> semantics, etc.).
> 
> Perhaps the suggestion is in fact one that has already been made but for 
> which I can't remember the conclusion: how about abandoning 'new' in 
> favor of more specific keywords/library templates that control whether 
> the instantiation occur on the heap or on the stack?


Yes, abandoning new if it would help. Objects on the heap could be managed by different garbage collectors (or with different settings, mark-sweep, precise/conservative, reference-counting, etc.).

I don't want to have to change the definition of the type. The instantiation is a separate concern to the implementation. It should be up to the user of a type to decide whether to allocate it on the stack or the heap and so on.

The compiler should be intelligent enough to see whether the class is derived or not and do its optmisations accordingly.


More information about the Digitalmars-d mailing list