Scala: What is the Nothing data type?

Scala FAQ: What is the Nothing type in Scala, and how do I use it?

Solution

In Scala, the Nothing type is called a bottom type, which means it is a sub-type of every other type in the Scala type system. It is also specifically a data type that has no instances.

In practical use, Nothing is used to indicate that a computation or function will never produce a result normally, either because it throws an exception, enters an infinite loop, or encounters some other abnormal termination.

Visually, this is what the Nothing type looks like in the Scala type hierarchy (image courtesy of this scala-lang.org page):

The Scala type hierarchy and the Nothing data type

Scala: Common uses of Nothing

Some common use cases of Nothing in Scala include:

  • Signals Abnormal Termination: As a programmer, this has been my most common use case. Nothing is used when a function is expected to return a specific type but encounters a condition where it can’t produce a valid result, so it returns Nothing to signal abnormal termination. For example, the throw keyword in Scala has the return type Nothing because it doesn’t return normally.

  • Type Inference: When the Scala compiler cannot infer the type of an expression, it may use Nothing as a fallback type. This often happens in situations where the inferred type needs to be a common supertype of multiple types, but there is no common supertype other than Nothing. (I’ve seen this quite a few times when I don’t declare my types, or do so improperly.)

  • Empty Collections: Per ChatGPT, some collection types, such as List or Option, can contain elements of any type. If a collection is known to be empty, its type can be inferred as List[Nothing] or Option[Nothing], indicating that it contains no elements. (I have not used this, but I asked ChatGPT this question, and it was one of its responses.)

Here’s an example of a Scala function that returns Nothing:

def fail(message: String): Nothing =
    throw new RuntimeException(message)

When I teach how to write your own linked list data type in Scala, I show how to use the Nothing type, like this:

case object Caboose extends LinkedList[Nothing]:
    def head: Nothing =
        throw new NoSuchElementException("Caboose; head is empty")
    def tail: LinkedList[Nothing] =
        throw new UnsupportedOperationException("Caboose; tail is empty")
    def isEmpty: Boolean = true

And if for some reason you want to create an empty List, this is one possible way of doing so:

val emptyList: List[Nothing] = Nil

That being said, when I create an empty List, I believe I’ve always done it by declaring a type, like these examples:

val emptyList: List[Int] = Nil
val emptyList: List[String] = Nil

The Nothing type in ZIO

One place where I see the Nothing data type is used a lot is in the Scala ZIO library.

Per this ZIO documentation page, it’s used with ZIO’s E parameter, which represents its potential failure type. Per that page, “If this type parameter is Nothing, it means the effect cannot fail, because there are no values of type Nothing.” You can see that in these examples from that page:

val someInt: ZIO[Any, Nothing, Option[Int]]     = ZIO.some(3)
val noneInt: ZIO[Any, Nothing, Option[Nothing]] = ZIO.none

When that text says “there are no values of type Nothing”, remember that there is a difference between a type and a value in Scala. A type is like Int or String, and values are like 42 and "hello".

Summary: Scala’s Nothing type

In summary, Nothing in Scala is used to indicate that a computation or function will never produce a result normally, either because it throws an exception, enters an infinite loop, or encounters some other abnormal termination. As shown in the ZIO example, Nothing is also used to indicate that an effect can’t fail, because there are no values of type Nothing.