Give struct the status it deserves
Stewart Gordon
smjg_1998 at yahoo.com
Mon Mar 27 07:23:58 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.
Or they are writing or using a D interface to a C or other foreign API.
Or they want to save the overhead of memory allocation by using a
lightweight alternative for those situations where the power of classes
isn't needed. Or they are interfacing a binary file format.
> 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.
No I don't. What are you talking about?
> 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.
What's wrong with that? Do you simply miss the word "new" in uses of
static opCall?
> 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.
If you want classes, you know where to find them.
> 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.
Why would anyone want to do that? What's wrong with simply
Qwert yuiop = asdfg; // to create a copy
Qwert* zxcvb = &asdfg; // to create a pointer
> 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.
That's an issue with those containers, rather than with structs themselves.
> 8. structs have different behaviours between array and Vector, array [] returns
> a reference, Vector [] returns a copy, nice rookie trap.
>
> 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"
>
> 2. add constructor for struct, destructors are not necessary.
>
> 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.
Maybe you could elaborate on how these reference semantics would work
such that they'd be any different from classes.
Stewart.
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS-
PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
More information about the Digitalmars-d
mailing list