Usages of Null / Nothing / Unit in Scala

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:

One other use of Nothing is as a return type for methods that never
return. It makes sense if you think about it. If a method’s return
type is Nothing, and there exists absolutely no instance of Nothing,
then such a method must never return.

Your logging method would return Unit. There is a value Unit so it can actually be returned. From the API docs:

Unit is a subtype of scala.AnyVal. There is only one value of type
Unit, (), and it is not represented by any object in the underlying
runtime system. A method with return type Unit is analogous to a Java
method which is declared void.

Leave a Comment