Give struct the status it deserves
Alexander Panek
alexander.panek at brainsware.org
Sun Mar 26 04:58:37 PST 2006
Hong wrote:
> Structs are second class citizens of... or maybe refugees of D, they are victims
> of discrimination.
> Following are the reasons:
>
> 1. most D programmers don't use struct, unless they happen to be the poor guy
> writing the C interface.
>
I don't know, but are you aware of what a struct is used for? It
encapsules data and provides an easy access to chunks of data related to
one bunch of information / object.
> 2. to use structs you have to use pointers, but the spec says pointers are only
> provided for C compatibility, structs are to be avoided as long as the world
> spins.
>
Who in the world told you not to use pointers? I haven't seen that in
the documentation, sorry.
> 3. pointers + overloaded operators make a mess.... this is a less offensive
> example: (*o) = (*t)[(*i) * (*j)] ... the pointers and operators are abusing
> each other.
>
> 4. structs cannot have constructor, "static opCall" is best you can do.
>
Take a look at std.boxer, there you have a constructor like
function/template.
> 5. structs are more efficient? Not when structs are passed around by value. To
> change a struct member: 1. make a copy 2. change the copy 3. Copy the copy back
> into the original location. Two damned copies for.... efficiency.
>
As said, I can't find anything in the documentation that says you should
not use pointers.
"Structs and unions are meant as simple aggregations of data, or as a
way to paint a data structure over hardware or an external type.
External types can be defined by the operating system API, or by a file
format. Object oriented features are provided with the class data type."
-- http://www.digitalmars.com//d/struct.html
> 6. yes, you can use pointers to change a struct member without copying, if you
> can be bothered to define 2 accessor methods, one returns a copy another one
> returns a pointer.
>
What's the problem here? Same goes with classes.
> 7. Most standard containers (DTL) do not allow a pointer to a contained struct
> to be retrieved, all changes, or just about anything has to be done via copies.
>
> 8. structs have different behaviours between array and Vector, array [] returns
> a reference, Vector [] returns a copy, nice rookie trap.
>
A Vector is no array, keep that in mind. There're still pointers, you
know? ;)
>
> Imho, following are little suggestions for structs to make first class citizens
> of D:
>
> 1. have some sort of reference semantic to replace pointers. Pointers are not
> necessary unless structs are used with "new"
>
Pointers *are* necessary. D is a high level language, of course, but
also provides the ability to write low level code. And structs, as they
*are*, are a very important part of this ability.
> 2. add constructor for struct, destructors are not necessary.
>
As said before.
> I use structs because I need efficiency and stack allocated data structures,
> however, it is painful to see a C++ program turns into a C like program when
> ported to D.
>
You can also provide effient code with classes, if you feel your code is
'ugly' with structs. Also, structs should not be abused. Structs are
structs and classes are classes - they both have their specific
purposes, one should not forget that.
Regards,
Alex
PS: No offense intended.
More information about the Digitalmars-d
mailing list