structs vs classes

biozic dransic at free.fr
Sat Jan 29 06:43:50 PST 2011


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?


More information about the Digitalmars-d mailing list