D vs. placement new (for classes) aka why D needs .sizeof and

Dan murpsoft at hotmail.com
Fri Apr 13 08:47:50 PDT 2007


I love D, one of the reasons being because Walter has provided unprecedented flexibility for structs; giving us almost all the features of classes without their drawbacks.

structs have operator overloading and methods.  : D

structs now only lack constructors, destructors, inheritance, interfaces, and private members (encapsulation).

I agree that constructors, destructors and interfaces are useful features... moving on;

~~
Inheritance:

Inheritance is something I tend to disagree with because: if one examines a class inheriting from another, there is no indication of the presence of inherited properties or methods within it's declaration other than the ': superClass' at the top.  Upon examining the superClass, you may then have to examine it's superClass and so forth; so finding out what members a class may have can take examining a dozen files.  This is the intended modus operandi.

I argue the better methodology is to implement interfaces, and architect the code such that few - if any - features are ever duplicated.  If need be, implementing a template is also an option.

Sure aristotle found that many things exhibit the same features and methods, I argue that redundancy is counter-productive to efficient algorithm execution; having no reflection on whether cats have fur and dogs do too.

~~
Encapsulation:

Oddly, Java Programmers(tm) tend to think that encapsulating a property and providing a simple getter and setter for it is somehow a good thing.  I argue that you're cluttering your NS with redundant information, and causing your code to jump into the getter and setter method every time you access a property without adding anything useful.

In open source programming, the only reason I can find to encapsulate is to provide a clearly defined external interface to allow implementation changes without affecting the implemented interface; i.e: allowing programmers to see into the box might lead one to take advantage of it's bugs.  Again, the usefulness of interfaces makes itself clear.

~~
Drawbacks of Classes:

Classes lack transparency.  You don't know the size, the location, the alignment... there is code and structure within them you aren't aware of, and much of it is often unneccassary.  You cannot intelligently manipulate the implementation of a class even internally - such as iterating through an array of them and accessing a property simply by adding the sizeof to an iterator.

You cannot pass a class by value, while a struct can be passed by value or reference.  You cannot easily duplicate an Object instanciated from a class.  You cannot declare an instanciated Object literal, but you can declare such a struct.

These shortcomings I feel are sufficient to warrant the use of structs unless you are implementing an interface which other programmers will use in their own code (read: library), or getters/setters/ctor/dtor are critical to the legible implementation of something such that a good-old-fashioned method on the struct won't do.

I'm looking forward to any replies.  
I want to learn any reasons why I might be wrong?



More information about the Digitalmars-d mailing list