Why don't we have two nulls?

后端 未结 27 1093
孤独总比滥情好
孤独总比滥情好 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 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);
    }
    

提交回复
热议问题