Karl's answer is good. Here is an additional use that I don't think anyone else has mentioned. The type of
if E then A else B
should be a type that includes all the values in the type of A
and all the values in the type of B
. If the type of B
is Nothing
, then the type of the if
expression can be the type of A
. I'll often declare a routine
def unreachable( s:String ) : Nothing = throw new AssertionError("Unreachable "+s)
to say that code is not expected to be reached. Since its type is Nothing
, unreachable(s)
can now be used in any if
or (more often) switch
without affecting the type of result. For example
val colour : Colour := switch state of
BLACK_TO_MOVE: BLACK
WHITE_TO_MOVE: WHITE
default: unreachable("Bad state")
Scala has such a Nothing type.
Another use case for Nothing
(as mentioned in Karl's answer) is List[Nothing] is the type of lists each of whose members has type Nothing. Thus it can be the type of the empty list.
Haskell does things a bit differently. In Haskell an expression that never produces a value can have the type forall a.a
. An instance of this type scheme will unify with any other type, so it effectively acts as a bottom type, even though (standard) Haskell has no notion of subtyping. For example the error
function from the standard prelude has type forall a. [Char] -> a
. So you can write
if E then A else error ""
and the type of the expression will be the same as the type of A
, for any expression A
.
The empty list in Haskell has the type forall a. [a]
. If A
is an expression whose type is a list type, then
if E then A else []
is an expression with the same type as A
. The type of []
is the same as the type [error ""]
, which goes to show that the empty list is not the only value of its type.
void
data... – Basile Starynkevitch yesterdayvoid
, and unit type must have one value. Also, as you pointed out, you cannot even declare a value of typevoid
, that means it's not even a type, just a special corner case in the language. – Sarge Borsch yesterdayvoid
in Java is nearly the same: not really a type & can't have values. – Sarge Borsch yesterdaynil
(a.k.a.,()
), which is a unit type. – Joshua Taylor 15 hours ago