Denis Koroskin wrote: > On Sun, 27 Sep 2009 03:01:48 +0400, Walter Bright > <newshou...@digitalmars.com> wrote: > >> Denis Koroskin wrote: >>> One more: >>> T foo(bool someCondition) >>> { >>> T? t; >>> if (someCondition) t = someInitializer(); >>> // ... >>> if (t.isNull) { // not initialized yet >>> // ... >>> } >>> return enforce(t); // throws if t is not initialized yet, >>> because foo *must* return a valid value by a contract >>> } >> >> It seems to me you've got null references there anyway? >> >> What would you do about: >> >> T[] a; >> a[i] = foo(); >> >> where you want to have unused slots be null (or empty, or nothing)? > > Easy: > > T? foo(); // returns valid object or a null > > T?[] a; > a[i] = foo();
The case of a non-null array is, I think, worthy of some more consideration. These are the things that would not be possible with a non-nullable array: - newing it - setting .length to a greater value - appending a nullable array of the same base type. Basically, anything that may fill it with nulls. The only two allowed instructions would be ~= NonNullable and ~= NonNullableArray. And it's good that way.