(non)nullable types

Alex Burton alexibu at mac.com
Mon Feb 9 02:38:50 PST 2009


I vote yes.

I was pretty sure the topic was refering to the ability to specify a pointer that could not be set to 0.

I totally agree with this rule, and enforce it in all my C++ code with smart pointers.

class X
{

};

X x; //in D
or X * x; //in C++

Setting x to zero or any other value that is not the address of an instance of X is bad bad bad, and should be discouraged at all costs.

If I ask for a pointer to an instance of X and you give me a zero or 0x0123456 or anything else, it is like mixing in a bicycle when a recipe asked for a cup of olive oil.

This is a huge source of bugs where people best described as C programmers do C++.

In D the mistake can be made especially when a struct changes into a class, and allocations are not changed. Like X x has to change to X x = new X (which is a bit repetitive in D IMHO).
I think the default should be to have the native X x (D pointer to class) to be instantiating the class, and the X x = new Y should only be required when X and Y are different (inherited) types and 
X x(32); when the class X has a ctor with arguments ( actually why don't I start a new thread about this)
There are many better ways to write code than using nullable pointers.



More information about the Digitalmars-d mailing list