Biggest problems w/ D
Tristam MacDonald
swiftcoder at gmail.com
Thu Aug 9 20:29:10 PDT 2007
Kirk McDonald wrote:
> C. Dunn wrote:
>
>> 5) Same syntax structs and classes. This is minor problem, but it is
>> extremely confusing to the novice. A struct and a class are
>> completely different in D, but the fact that I can write code the
>> same to use either one implies too much similarity. It leads to
>> programming errors which easily result in seg-fault.
>>
>> Also, it is too difficult to switch a set of objects from structs to
>> classes or vice versa. I need to be able to do that in order to
>> compare runtimes for various implementations. Again, I don't know
>> the best answer.
>>
>
> Structs and classes are very different beasts in D, even if they look
> the same. However, there are some important differences in their use
> which should clue you in:
>
> Using 'new' on a class gives you a reference, using it on a struct gives
> you a pointer.
>
> Structs are plain ol' data. Saying "SomeStruct s;" gives you an instance
> you can immediately behind using. You have to use 'new' to get a class
> instance.
>
> Classes have inheritance and all those other object-oriented features,
> while structs do not.
>
> What I am getting at is that whether something should be a class or a
> struct seems like an important design decision which shouldn't be
> expected to change after development is well underway. Also, code
> doesn't really look all that similar when it uses structs vs. classes.
>
I don't agree here. I had all my algebraic classes (vectors, matrices,
transforms, etc.) as structs, as they needed to be value types, and for
performance. However, I had to convert several of the more complex
structs to classes instead, when I needed reference semantics.
Since structs use a different constructor syntax, along with 'this'
versus '*this', it was quite annoying. Since the only difference the
original spec highlighted was that one in a reference type, and one a
value type, it seems silly to have large syntactical differences between
them. In particular, that structs lack meaningful
constructors/destructors/copying semantics is *really* annoying. Value
types are scoped, so why not let us use that to our advantage?
More information about the Digitalmars-d
mailing list