为什么在Erlang中nil>0为真?

rkue9o1l  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(175)

Erlang is the first language I come across to give true for nil > 0.
What is the story behind this decision?
Other languages seem to behave differently.
Python:

None > 0
# False

JavaScript:

null > 0
// false

Ruby:

nil > 0
NoMethodError: undefined method `>' for nil:NilClass
66bbxpm5

66bbxpm51#

在Erlang中,任何术语都可以与任何其他术语进行比较。Erlang术语比较的顺序为:

number < atom < reference < fun < port < pid < tuple < map < nil < list < bit string

因此nil〉0true有关期限比较的更多信息

inn6fuwd

inn6fuwd2#

Yes, in Erlang, nil > 0 evaluates to true. But when Erlang programmer talks about "nil", he means the empty list: [] . If you write nil in Erlang code, it's just an atom 'nil' , like 'dog' or 'hello' . And any atom is larger than any number because of the following built-in hierarchy:

number < atom < reference < fun < port < pid < tuple < map < nil < list < bit string

But it's very important to note that "nil" in this line refers to empty list [] , not to the atom 'nil' .
As a side note: Evaluating [] > 0 also returns true because any number is smaller than nil.
And finally, [] > nil also returns true because nil ( [] ) is larger than any atom ( 'nil' ).

相关问题