Spec#, nullables and more

Adam Burton adz21c at gmail.com
Fri Nov 5 17:23:23 PDT 2010


Walter Bright wrote:

> Denis Koroskin wrote:
>> On Fri, 05 Nov 2010 23:44:58 +0300, Walter Bright
>> <newshound2 at digitalmars.com> wrote:
>>>
>>> To eliminate null pointers is the same as shooting the canary in your
>>> coal mine because its twitter annoys you.
>> 
>> I'm tired of pointing out that NO ONE is talking about eliminating null
>> pointers, but rather extending an existing type system to support
>> non-nulls. Your hate towards non-nullables comes from misunderstanding
>> of the concept.
> 
> Consider non-nullable type T:
> 
>    T[] a = new T[4];
>    ... time goes by ...
>    T[1] = foo;
>    T[3] = bar;
>    ... more time goes by ...
>    bar(T[2]);
> 
> In other words, I create an array that I mean to fill in later, because I
> don't have meaningful data for it in advance. What do I use to default
> initialize it with non-nullable data? And once I do that, should bar(T[2])
> be an error? How would I detect the error?
> 
> In general, for a non-nullable type, how would I mark an instance as not
> having meaningful data?
> 
> For example, an int is a non-nullable type. But there's no int value that
> means "no meaningful value", and this can hide an awful lot of bugs.
> 
> I'm not sure at all that non-nullable types do more than make easy to find
> bugs much, much harder to find.

My understanding on what everyone is requesting is below.

class T
{
    private string x;
    public this(string xin) { x = xin; }
    public void doSomething() { writeln(x); }
}

//////Currently////////

function doSomething(T myT)
in
{
    Assert(myT is not null);	// NPE during debugging on a[3]
}
{
   myT.doSomething();	// "1", "2", "null", NPE during release
}

T[] a = new T[4];
a[0] = new T("1");
a[1] = new T("2");
a[2] = new T(null);

foreach(i; a)
   doSomething(i);

////// With NonNullables. Assume toNonNullable() is override to allow 
nullables to assign to nonNullables ////

function doSomething(T myT)	// the in is now redundant, function 
signature says no nulls allowed
{
   myT.doSomething();
}

T?[] a = new T?[4];	// Nullable array items
a[0] = new T("1");
a[1] = new T("2");
a[3] = new T(null);	// Compile error, null assignment to non-nullable

foreach(i; a)
   doSomething(i);	// Compile error potential null assignment to non-
nullable, please check for null (e.g. if statement) or override

foreach(i; a)
   doSomething(toNonNullable(i));	// "1", "2", NPE (toNonNullable checks for 
null and NPE if it is null. However we fail here when attempting to send a 
null into code that does not allow nulls instead of later.

In theory the altered doSomething is saying "don't give me meaningless 
data". Where as the array says "at this point I don't know enough so I'll 
take nulls in the mean time". However what we have done is shrink the amount 
of code a null can live in offering more guarantuees about doSomething.

Am I going along the right lines with that?



More information about the Digitalmars-d mailing list