Why don't we have two nulls?

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

    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.

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

    Existence of value:

    • Python: vars().has_key('variableName')
    • PHP: isset(variable)
    • JavaScript: typeof(variable) != 'undefined'
    • Perl: (variable != undef) or if you wish: (defined variable)

    Of course, when variable is undefined, it's not NULL

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

    You can always create an object and assign it to same static field to get a 2nd null.

    For example, this is used in collections that allow elements to be null. Internally they use a private static final Object UNSET = new Object which is used as unset value and thus allows you to store nulls in the collection. (As I recall, Java's collection framework calls this object TOMBSTONE instead of UNSET. Or was this Smalltalk's collection framework?)

    0 讨论(0)
提交回复
热议问题