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:49

VB6

  • Nothing => "There is no value."
  • Null = > "I don't know what the value is" - Same as DBNull.Value in .NET
查看更多
迷人小祖宗
3楼-- · 2020-05-20 01:51

It would be easy enough to create a static constant indicating unknown, for the rare cases when you'd need such a thing.

var apple = Apple.Unknown;
while (apple == Apple.Unknown) {} // etc
查看更多
SAY GOODBYE
4楼-- · 2020-05-20 01:51

Given how long it took Western philosophy to figure out how it was possible to talk about the concept of "nothing"... Yeah, I'm not too surprised the distinction got overlooked for a while.

查看更多
登录 后发表回答