Why don't we have two nulls?

后端 未结 27 1090
孤独总比滥情好
孤独总比滥情好 2021-02-02 08:38

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\"

相关标签:
27条回答
  • 2021-02-02 08:58

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

    0 讨论(0)
  • 2021-02-02 09:01

    Note null is an acceptable, yet known condition. An unknown state is a different thing IMO. My conversation with Dan in the comments' section of the top post will clarify my position. Thanks Dan!.

    What you probably want to query is whether the object was initialized or not.

    Actionscript has such a thing (null and undefined). With some restrictions however.

    See documentation:

    void data type

    The void data type contains only one value, undefined. In previous versions of ActionScript, undefined was the default value for instances of the Object class. In ActionScript 3.0, the default value for Object instances is null. If you attempt to assign the value undefined to an instance of the Object class, Flash Player or Adobe AIR will convert the value to null. You can only assign a value of undefined to variables that are untyped. Untyped variables are variables that either lack any type annotation, or use the asterisk (*) symbol for type annotation. You can use void only as a return type annotation.

    0 讨论(0)
  • 2021-02-02 09:02

    Why stop at two?

    When I took databases in college, we were told that somebody (sorry, don't remember the name of the researcher or paper) had looked at a bunch of db schemas and found that null had something like 17 different meanings: "don't know yet", "can't be known", "doesn't apply", "none", "empty", "action not taken", "field not used", and so on.

    0 讨论(0)
  • 2021-02-02 09:03

    It's because Null is an artifact of the language you're using, not a programmer convenience. It describes a naturally occurring state of the object in the context in which it is being used.

    0 讨论(0)
  • 2021-02-02 09:04

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

    0 讨论(0)
  • 2021-02-02 09:04

    Some people will argue that we should be rid of null altogether, which seems fairly valid. After all, why stop at two nulls? Why not three or four and so on, each representing a "no value" state?

    Imagine this, with refused, null, invalid:

    var apple;
    
    while (apple is refused)
    {
        askForApple();
    }
    
    if (apple is null)
    {
        sulk();
    }
    else if(apple is invalid)
    {
        discard();
    }
    else
    {
        eatApple(apple);
    }
    
    0 讨论(0)
提交回复
热议问题