References in D

Henning Pohl henning at still-hidden.de
Sat Sep 15 06:55:01 PDT 2012


On Saturday, 15 September 2012 at 13:36:00 UTC, Maxim Fomin wrote:
> On Saturday, 15 September 2012 at 12:43:22 UTC, Alex Rønne 
> Petersen wrote:
>> But this being said, I agree that references being nullable by 
>> default is hurtful. It allows any object reference to have an 
>> invalid state even though in 99% of cases, that doesn't make 
>> sense. It's a giant hole in the type system that many new 
>> languages have gotten rid of very early (forcing the 
>> programmer to use explicit option/nullable types).
>
> Are speaking about classes? Then how they can be initialized 
> (except for null and other existing object)?

When you want it to be initialized with null, use a pointer. Else 
you can use something like this:

class AClass {
	this(BClass bclass = new BClass) {
		_class = bclass;
	}
	
	BClass _class;	
}

By the way, a pointer holds two pieces information:
1) If there is an object available
2) If so, the object itself

In most cases, you only need the second one and the if is 
redunant.


More information about the Digitalmars-d mailing list