Why don't we have two nulls?

2020-05-20 01:04发布

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" and the more assertive "There is no value.".

There have been several cases where I'd have liked to differentiate between the two (especially when working with user-input and databases).

I imagine the following, where we name the two states unknown and null:

var apple;

while (apple is unknown)
{
    askForApple();
}

if (apple is null)
{
    sulk();
}
else
{
    eatApple(apple);
}

Obviously, we can get away without it by manually storing the state somwhere else, but we can do that for nulls too.

So, if we can have one null, why can't we have two?

27条回答
三岁会撩人
2楼-- · 2020-05-20 01:41

In my programming, I recently adopted the practice of differentiating "language null" and "domain null".

The "language null" is the special value that is provided by the programming language to express that a variable has "no value". It is needed as dummy value in data structures, parameter lists, and return values.

The "domain null" is any number of objects that implement the NullObject design pattern. Actually, you have one distinct domain null for each domain context.

It is fairly common for programmers to use the language null as a catch-all domain null, but I have found that it tends to make code more procedural (less object oriented) and the intent harder to discern.

Every time to want a null, ask yourself: is that a language null, or a domain null?

查看更多
Viruses.
3楼-- · 2020-05-20 01:43

If you are using .NET 3.0+ and need something else, you might try the Maybe Monad. You could create whatever "Maybe" types you need and, using LINQ syntax, process accordingly.

查看更多
倾城 Initia
4楼-- · 2020-05-20 01:43

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

查看更多
成全新的幸福
5楼-- · 2020-05-20 01:45

Isn't is bad enough that we have one null?

查看更多
虎瘦雄心在
6楼-- · 2020-05-20 01:45

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

查看更多
爷的心禁止访问
7楼-- · 2020-05-20 01:45

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.

查看更多
登录 后发表回答