On Sat, 06 Nov 2010 02:41:25 +0300, Walter Bright
<[email protected]> wrote:
Denis Koroskin wrote:
On Fri, 05 Nov 2010 23:44:58 +0300, Walter Bright
<[email protected]> 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?
How is T non-nullable? By the time you call bar(T[2]) T[2] is not
initialized yet, i.e. null. As such, T[]
This is clearly not a use-case for non-nullables. In your use case this
array contains un-initialized values for some time. Non-nullables means
that there is no such state as unitialized. As such, you use T?[], not T[].
If you need an array of elements that are never null, use the following:
T[] a = [new T1(), new T2(), new T3()];
or the following:
T?[] nullables = ...;
// setup a
T[] nonNullables = toNonnull(nullables); // or cast(T[])nullables;
similar to the way you create mutable objects and then cast to immutable.
In general, for a non-nullable type, how would I mark an instance as not
having meaningful data?
Err... what? By definition, a non-nullable is an object that can NEVER
hold meaningless data. If T must contain meaningless data, use T?, not T.
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.
Use int?, which is a special type that has an additional "not initialized
yet" value. This comes at a cost of a larger size though - there is simply
no other way to do it.
I'm not sure at all that non-nullable types do more than make easy to
find bugs much, much harder to find.
You are simply wrong. You clearly misunderstand the concept.