I just read this question and stumbled upon the following quote:
Scala treats
==
as if it were defined as follows in classAny
:final def == (that: Any): Boolean = if (null eq this) (null eq that) else (this equals that)
The (null eq this)
part made me wonder: Is it actually possible to call methods on null pointers? Can this
be null
in Scala?
I'm pretty new to Scala, but the only way I see that as being possible is due to the fact that "null" itself is an instance of Null, and not exactly a special value like "null" in Java.
http://blog.sanaulla.info/2009/07/12/nothingness/
This article helped me understand this a bit better.
Check out the Scala language specification, namely 6.3 The Null Value chapter:
This means that semantically when you compare something with
null
literal ornull
literal with something you are actually referring to method of a specialscala.Null
class. Treatnull
literal as a shorthand for that class.Of course at the implementation level it is optimized and ordinary
null
is used.null
is the only instance ofNull
class and it's a valid object.Null
is a subtype of all reference types.