Why don't we have two nulls?

后端 未结 27 1149
孤独总比滥情好
孤独总比滥情好 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条回答
  •  旧时难觅i
    2021-02-02 08:45

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

提交回复
热议问题