In how many languages is Null not equal to anything not even Null?
7 Answers
It's this way in SQL (as a logic language) because null means unknown/undefined.
However, in programming languages (like say, C++ or C#), a null pointer/reference is a specific value with a specific meaning -- nothing.
Two nothings are equivilent, but two unknowns are not. The confusion comes from the fact that the same name (null) is used for both concepts.
-
There are some programming languages with null-propagation. For instance VB.Net msmvps.com/blogs/bill/archive/2007/09/07/c-defies-logic.aspx– MarkJMar 8, 2010 at 12:18
In VB6 the expression Null = Null
will produce Null
instead of True
as you would expect.
This will cause a runtime error if you try to assign it to a Boolean, however if you use it
as the condition of "If ... Then
" it will act like False
. Moreover Null <> Null
will also
produce Null
, so:
In VB6 you could say that Null
is neither equal to itself (or anything else), nor unequal!
You're supposed to test for it using the IsNull()
function.
VB6 also has other special values:
Nothing
for object references.Nothing = Nothing
is a compile error. (you're supposed to compare it using "is
")Missing
for optional parameters which haven't been given. It has no literal representation so you can't even writeMissing = Missing
. (the test isIsMissing(foo)
)Empty
for uninitialized variables. This one does test equal to itself although there's also a functionIsEmpty()
.- ... let me know if I've forgotten one
I remember being a bit disgusted with VB.
-
1VB.Net has null propagation too. Null propagation is confusing, but if you're going to have nullable types at all, it's arguable that the cat is out of the bag, and you should propagate nulls. msmvps.com/blogs/bill/archive/2007/09/07/c-defies-logic.aspx– MarkJMar 8, 2010 at 12:19
Oracle is this way.
SELECT * FROM dual WHERE NULL=null; --no rows returned
-
2Actually, that is a property of SQL in general, it's not specific to Oracle.– sleskeMar 22, 2010 at 10:24
MySQL has a null-safe equality operator, <=>, which returns true if both sides are equal or both sides are null. See MySQL Docs.
-
+1 Interesting, but this should probably had been a comment on an SQL answer– WillbillOct 14, 2009 at 11:17
In C#, Nullable<bool> has interesting properties with respect to logical operators, but the equality operator is the same as other types in that language (i.e., ((bool?)null == (bool?)null) == true).
To preserve the short-circuited behavior of the short-circuited logical operators, and to preserve consistency with the non-short-circuited logical operators, the nullable boolean has some interesting properties. For example: true || null == true. false && null == false, etc. This stands in direct contradiction with other three-valued logic languages such as ANSI SQL.
-
There's other interesting behaviours in C#. For some values of x and y
x==y
is true butx<=y
is false. Which seems rather hard to defend. msmvps.com/blogs/bill/archive/2007/09/07/c-defies-logic.aspx– MarkJMar 8, 2010 at 12:22
You can make ruby work that way:
class Null
def self.==(other);false;end
end
n=Null
print "Null equals nothing" if n!=Null
-
@Hugh: Well, one might say that, but it is possible, which means people will do it... whether that's good or bad is another matter.– sleskeMar 22, 2010 at 10:25
-
In ruby, null is called nil and NULL would not be defined giver the code posted. It is however perfectly possible to do what is implied here by typing:
class NilClass; def ==(o); false; end; end
Nov 7, 2010 at 21:16 -
Wow @ormuriauga - you caught a 2 year old typo. Fixed. Now it does create a
Null
which is not equal to itself, as the question asked.– AShellyNov 8, 2010 at 17:58
In SQL you would have to do something like:
WHERE column is NULL
rather than
WHERE column = NULL