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

Within PHP Strict you need to do an isset() check for set variables (or else it throws a warning)

if(!isset($apple))
{
    askForApple();
}

if(isset($apple) && empty($apple))
{
    sulk();
}
else
{
    eatApple();
}
查看更多
倾城 Initia
3楼-- · 2020-05-20 01:45

Two nulls would be the wrongest answer around. If one null is not enough, you need infinity nulls.

Null Could mean:

  • 'Uninitialized'
  • 'User didn't specify'
  • 'Not Applicable here, The color of a car before it has been painted'
  • 'Unity: This domain has zero bits of information.'
  • 'Empty: this correctly holds no data in this case, for example the last time the tires were rotated on a new car'
  • 'Multiple, Cascading nulls: for instance, the extension of a quantity price when no quantity may be specified times a quantity which wasn't specified by the user anyway'

And your particular domain may need many other kinds of 'out of band' values. Really, these values are in the domain, and need to have a well defined meaning in each case. (ergo, infinity really is zero)

查看更多
Deceive 欺骗
4楼-- · 2020-05-20 01:47

In haskell you can define something like this:

data MaybeEither a b = Object a
                     | Unknown b
                     | Null
                       deriving Eq
main = let x = Object 5 in
       if x == (Unknown [2]) then putStrLn ":-("
       else putStrLn ":-)"

The idea being that Unknown values hold some data of type b that can transform them into known values (how you'd do that depends on the concrete types a and b).

The observant reader will note that I'm just combining Maybe and Either into one data type :)

查看更多
Animai°情兽
5楼-- · 2020-05-20 01:47

For me null represents lack of value, and I try to use it only to represent that. Of course you can give null as many meanings as you like, just like you can use 0 or -1 to represent errors instead of their numerical values. Giving various meanings to one representation could be ambiguous though, so I wouldn't recommend it.

Your examples can be coded like apple.isRefused() or !apple.isValid() with little work; you should define beforehand what is an invalid apple anyway, so I don't see the gain of having more keywords.

查看更多
迷人小祖宗
6楼-- · 2020-05-20 01:48

javascript actually has both null and undefined (http://www.w3schools.com/jsref/jsref_undefined.asp), but many other languages don't.

查看更多
放我归山
7楼-- · 2020-05-20 01:49

It's been tried: Visual Basic 6 had Nothing, Null, and Empty. And it led to such poor code, it featured at #12 in the legendary Thirteen Ways to Loathe VB article in Dr Dobbs.

Use the Null Object pattern instead, as others have suggested.

查看更多
登录 后发表回答