Why don't we have two nulls?

2020-05-20 01:04发布

I've often wondered why languages with a null representing "no value" don't differentiate between the passive "I don't know what the value is" and the more assertive "There is no value.".

There have been several cases where I'd have liked to differentiate between the two (especially when working with user-input and databases).

I imagine the following, where we name the two states unknown and null:

var apple;

while (apple is unknown)
{
    askForApple();
}

if (apple is null)
{
    sulk();
}
else
{
    eatApple(apple);
}

Obviously, we can get away without it by manually storing the state somwhere else, but we can do that for nulls too.

So, if we can have one null, why can't we have two?

27条回答
爷、活的狠高调
2楼-- · 2020-05-20 01:30
boolean getAnswer() throws Mu
查看更多
Animai°情兽
3楼-- · 2020-05-20 01:31

In most programming languages null means "empty" or "undefined". "Unknown" on the other hand is something different. In essence "unknown" describes the state of the object. This state must have come from somewhere in your program.

Have a look at the Null Object pattern. It may help you with what you are trying to achieve.

查看更多
4楼-- · 2020-05-20 01:33

The problem is that in a strongly typed language these extra nulls are expected to hold specific information about the type.

Basically your extra null is meta-information of a sort, meta-information that can depend on type.

Some value types have this extra information, for instance many numeric types have a NaN constant.

In a dynamically typed language you have to account for the difference between a reference without a value (null) and a variable where the type could be anything (unknown or undefined)

So, for instance, in statically typed C# a variable of type String can be null, because it is a reference type. A variable of type Int32 cannot, because it is a value type it cannot be null. We always know the type.

In dynamically typed Javascript a variable's type can be left undefined, in which case the distinction between a null reference and an undefined value is needed.

查看更多
闹够了就滚
5楼-- · 2020-05-20 01:36

As to why we don't have two nulls, is it down to the fact that, historically in C, NULL was a simple #define and not a distinct part of the language at all?

查看更多
够拽才男人
6楼-- · 2020-05-20 01:38

Some people are one step ahead of you already. ;)

查看更多
beautiful°
7楼-- · 2020-05-20 01:39

The Null type is a subtype of all reference types - you can use null instead of a reference to any type of object - which severely weakens the type system. It is considered one of the a historically bad idea by its creator, and only exists as checking whether an address is zero is easy to implement.

查看更多
登录 后发表回答