(non)nullable types

Denis Koroskin 2korden at gmail.com
Sat Feb 14 15:27:22 PST 2009


On Sun, 15 Feb 2009 02:22:17 +0300, Christopher Wright <dhasenan at gmail.com> wrote:

> Nick Sabalausky wrote:
>> You would only need to check when you're going to derefernce or convert  
>> to a non-nullable. Nulls could still be stored and passed around  
>> without checking. If this doesn't cover what you're concerned about,  
>> perhaps you could provide an example?
>
> Let's say I originally have this:
>
> class Foo {
> 	void delegate()? dg;
> 	void doStuff() {
> 		if (dg) {
> 			// some long code path with code duplicated from
> 			// other methods in Foo
> 		} else {
> 			// some other long code path
> 		}
> 	}
> }
>
> Now I want to refactor that:
>
>
> class Foo {
> 	void delegate()? dg;
> 	void doStuff() {
> 		if (dg) {
> 			doPart1;
> 			doPart2;
> 		} else {
> 			// some other long code path
> 		}
> 	}
>
> 	private void doPart1() {
> 		// use dg; why should I check?
> 	}
> 	private void doPart2() {
> 		// use dg; why should I check?
> 	}
> }
>
> Small examples don't show it very well.

private void doPart1()
{
    void delegate() dg = unchecked(this.dg); // no checking done
    // use dg here without checking
    // *BUT* it someone denies the contract and calls the method without
    // ensuring that this.dg is not null, you'll get a access violation (or NPE)
}






More information about the Digitalmars-d mailing list