Null references redux

language_fan foo at bar.com.invalid
Sat Sep 26 15:25:26 PDT 2009


Sun, 27 Sep 2009 02:15:33 +0400, Denis Koroskin thusly wrote:

> Until the, non-nullable references are too hard to use to become
> useful, because you'll end up with a lot of initializer functions:
> 
> void foo(int a) {
> 	Object initializeFoo() {
> 		if (a == 1) return new Object1();
> 		if (a == 2) return new Object2();
> 		return new Object3();
>          }
> 
> 	Object foo = initializeFoo();
> 	foo.doSomething();
> }
> 
> I actually believe the code is more clear that way, but there are cases
> when you can't do it (initialize a few variables, for example)

Having a functional switch() helps a lot. I write code like this every 
day:

  val foo = predicate.match {
    case 1 => new Object1
    case 2 => new Object2("foo", "bar")
    case _ => new DefaultObject
  }

  foo.doSomething

I also rarely have runtime bugs these days.



More information about the Digitalmars-d mailing list