Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 4 years ago.
Does anybody know any good reference/s?
Thanks, Tim
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 4 years ago.
Does anybody know any good reference/s?
Thanks, Tim
Unit
- essentially a type that has only one value, ()
- it is used in places where you would normally use void
in Java.Null
- another type that has only one value, null
. It is a subtype of all reference (nullable) types.Nothing
- a type that has no values and is a subtype of all other types. The only valid expression that has type Nothing
is an expression that always throws an exception. Nothing
is useful to represent things like for example, an empty list, which has a type List[Nothing]
. Thanks to that, it can be used everywhere where a List[E]
is required, regardless of type E
(this is also powered by list's covariance).Scala.Null is the type of a "null" reference. It is a reference type none the less (and so an object of type Null can not be assigned to a value type variable like a Long, which is not a reference type). Think of it as an empty or invalid reference.
Scala.Nothing is a class that does not hold any value. You can use it to signal an empty result (instead of using null as you would do in many other languages). It is a subtype of any type in Scala (so you can assign a Nothing to any type variable).
Scala.Unit is what other languages might call a "void" data type: it represents the absence of any value. Consequently, "()" is a shorthand for an instance of this class (in fact, there is just one instance of this class).
All this is explained nicely in Martin Odersky's book "Programming in Scala".