Why don't we have two nulls?

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

    The problem is that in a strongly typed language these extra nulls are expected to hold specific information about the type.

    Basically your extra null is meta-information of a sort, meta-information that can depend on type.

    Some value types have this extra information, for instance many numeric types have a NaN constant.

    In a dynamically typed language you have to account for the difference between a reference without a value (null) and a variable where the type could be anything (unknown or undefined)

    So, for instance, in statically typed C# a variable of type String can be null, because it is a reference type. A variable of type Int32 cannot, because it is a value type it cannot be null. We always know the type.

    In dynamically typed Javascript a variable's type can be left undefined, in which case the distinction between a null reference and an undefined value is needed.

提交回复
热议问题