Proposal : allocations made easier with non nullable types.

Ary Borenszweig ary at esperanto.org.ar
Mon Feb 9 03:41:59 PST 2009


Alex Burton wrote:
> I think it makes no sense to have nullable pointers in a high level language like D.
> 
> In D :
> 
> X x = new X;
> This is a bit redundant, if we take away the ability to write X x; to mean X x = 0; then we can have X x; mean X x = new X;
> If the class has a ctor then we can write X x(32); instead of X x = new X(32);
> Only when the types of the pointer and class are different do we need to write X x = new Y;
> We can do this syntactically in D because classes cannot be instantiated on the stack (unless scope is used, which I have found a bit pointless, as members are not scope so no deterministic dtor)
> 
> This makes the code much less verbose and allows code to change from X being a struct to X being a class without having to go around and change all the X x; to X = new X;
> 
> As I said in the nullable types thread:
> Passing 0 or 0x012345A or anything else that is not a pointer to an instance of X to a variable declared as X x is the same as mixing in a bicycle when a recipe asks for a cup of olive oil.
> 
> There are much better, and less error prone ways to write code in a high level language than allowing null pointers.
> 
> Alex

How would you do this?

X x;

if(someCondition) {
   x = new SomeX();
} else {
   x =  new SomeOtherX();
}



More information about the Digitalmars-d mailing list