Spec#, nullables and more

Jonathan M Davis jmdavisProg at gmx.com
Fri Nov 5 17:26:03 PDT 2010


On Friday, November 05, 2010 16:41:25 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.

I thought that the point of non-nullable types were that they _always_ had to 
have meaningful data. I certainly see no point in a type which just uses a value 
other than null to indicate that it doesn't have meaningful data. That's just 
another type of null.

Now, I think that your example is a perfect example of one of the reasons why 
you  do _not_ want to have to use non-nullable types. There are cases where you 
_know_ that a variable should never be null, and having that guarantee can be 
very useful. However, there are plenty of other cases where you _need_ to be 
able to have a variable be null, even if it isn't null normally. So, while I 
wouldn't necessarily be opposed to adding support for non-nullable references 
and/or pointers to D, I would very much be opposed to making _all_ pointers or 
references be non-nullable. For that matter, I'd be opposed to it even becoming 
the default.

So, I can understand wanting non-nullable pointers and references, and I'm not 
really opposed to them being supported, but I wouldn't want to have to use them 
or have to bend over backwards to get properly nullable pointers and references.

- Jonathan M Davis


More information about the Digitalmars-d mailing list