struct inheritance need?

Yigal Chripun yigal100 at gmail.com
Tue Dec 16 14:17:37 PST 2008


Weed wrote:
> bearophile пишет:
>  > Weed:
>  >> Planned in the future to implement inheritance of structs or the
> static creation of classes?
>  >
>  > Inheritance of structs: I think it's not planned. Structs in D are
> meant to be used for different things than classes.
>  > Yet, as time passes structs are gaining more power: you can't believe
>  >
> that very recently they have gained constructors/destructors too in D2.
> Probably in a system language conceptual purity isn't much appreciated :-)
>  >
>
> I believe that the opportunity to place an object in memory, stack or
> heap is more important than the struggle against "splicing".
>
> I think not worth taking structs and classes from C#. May be bytecode
> interpreter C# does not show the difference in speed between the
> allocation of memory by a small object + its using and the use of a
> static object, so developers C# decided to do so as done. (but I am not
> a specialist in the design of compilers :))
>
>  > Static creation of classes (I think you mean creation of objects): it
>  > sounds like an interesting thing, I know of a system language that
>  > allows the creation of objects only at compile-time, and at
>  > compile-time it also performs several space optimizations among such
>  > objects (and such space optimizations often improve running speed a
>  > little).
>
> And in fact we come to making structs and classes similar except that
> classes can not be assigned by value.
>
> Such an option I like.

If I understand you correctly - I think you confuse here two separate 
and orthogonal issues.
1) struct vs. class
2) memory allocation

What D tries to do is to provide types with value semantics via structs 
and types with reference semantics _and_polymorphism_ via classes.
IMO C++ is a prime example how to not design a language and the slicing 
problem is a good example of that. value types should not have 
polymorphism whatsoever as is done in D.

memory allocation is orthogonal to this:
class C {...}
Struct S {...}

auto c1 = new C; // classes default to heap alloc
scope c2 = new C; // c2 is on stack
S s1; // structs default to stack
auto s2 = new S; // s2 is S* (heap alloc)

I think Andrei said he wants to make changes to "scope", that is IIRC.

struct inheritance, if it gets implemented in D, should express IMO 
semantics similar to concepts as in C++0x, not provide polymorphism for 
value types which is clearly a mistake. (also this will remove the need 
for typedefs, isn't it?)


More information about the Digitalmars-d-learn mailing list