I've just read: http://oldfashionedsoftware.com/2008/08/20/a-post-about-nothing/
As far as I understand, Null
is a trait and its only instance is null
.
When a method takes a Null argument, then we can only pass it a Null
reference or null
directly, but not any other reference, even if it is null (nullString: String = null
for example).
I just wonder in which cases using this Null
trait could be useful.
There is also the Nothing trait for which I don't really see any more examples.
I don't really understand either what is the difference between using Nothing and Unit as a return type, since both doesn't return any result, how to know which one to use when I have a method that performs logging for example?
Do you have usages of Unit / Null / Nothing as something else than a return type?
The article you quote can be misleading. The
Null
type is there for compatibility with the Java virtual machine, and Java in particular.We must consider that Scala:
null
references to access, for example, Java libraries and codethus it becomes necessary to define a type for the
null
value, which is theNull
trait, and hasnull
as its only instance.There is nothing especially useful in the
Null
type unless you're the type-system or you're developing on the compiler. In particular I can't see any sensible reason to define aNull
type parameter for a method, since you can't pass anything butnull
In terms of category theory Nothing is an initial object and Unit is a terminal object.
https://en.wikipedia.org/wiki/Initial_and_terminal_objects
Initial objects are also called coterminal or universal, and terminal objects are also called final.
If an object is both initial and terminal, it is called a zero object or null object.
You only use Nothing if the method never returns (meaning it cannot complete normally by returning, it could throw an exception). Nothing is never instantiated and is there for the benefit of the type system (to quote James Iry: "The reason Scala has a bottom type is tied to its ability to express variance in type parameters."). From the article you linked to:
Your logging method would return Unit. There is a value Unit so it can actually be returned. From the API docs:
if you use
Nothing
, there is no things to do (include print console) if you do something, use output typeUnit
... then how to use
Nothing
?Here's an example of
Nothing
fromscala.predef
:In case you're unfamiliar (and search engines can't search on it)
???
is Scala's placeholder function for anything that hasn't been implemented yet. Just like Kotlin'sTODO
.You can use the same trick when creating mock objects: override unused methods with a custom
notUsed
method. The advantage of not using???
is that you won't get compile warnings for things you never intend to implement.I've never actually used the
Null
type, but you useUnit
, where you would on java usevoid
.Nothing
is a special type, because as Nathan already mentioned, there can be no instance ofNothing
.Nothing
is a so called bottom-type, which means, that it is a sub-type of any other type. This (and the contravariant type parameter) is why you can prepend any value toNil
- which is aList[Nothing]
- and the list will then be of this elements type.None
also if of typeOption[Nothing]
. Every attempt to access the values inside such a container will throw an exception, because that it the only valid way to return from a method of typeNothing
.