Give struct the status it deserves
Norbert Nemec
Norbert at Nemec-online.de
Tue Mar 28 02:28:04 PST 2006
I partly agree with you.
1) A constructor for structs would definitely be necessary: Currently, a
struct is always initialized to all-zero fields. The "static opCall" is
nice syntactic sugar for changing the contents of the struct afterwards,
but it does not allow a guaranteed initialization, since D does not
force you to call opCall.
2) Reference handling similar to C++ would be very desirable. Of course,
a pointer can always be used instead, but it makes very ugly code and is
probably one of the reasons why structs are used so rarely in D.
3) Yes, classes are inefficient when used for purposes that should be
done with a struct. I have seen implementations of 3D-vector arithmetics
in D where the vector was defined as class. This means: each vector has
to be allocated on the heap!!! The benchmark example that you give may
be artificial but it is not at all unrealistic to have a loop that
allocates and deallocates 100000 small data structures. It makes a
tremendous difference whether this is done on the heap or on the stack!
It is good that D has a clear distinction between classes and structs,
but structs should be viewed as an important concept and given all the
power possible. Saying "use a class instead" is hardly ever an
acceptable excuse for lacking power in structs.
Greetings,
Norbert
More information about the Digitalmars-d
mailing list