Usages of Null / Nothing / Unit in Scala

scala return nothing
scala option(null)
scala null example
scala long null
scala null string
null nil in scala
scala type hierarchy
null check in scala

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?

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.

Scala, How to use regular expression in Java? How to parse number string with commas to Integer or Long? Using Websocket with Spring Framework  The reference types such as Objects, and Strings can be nulland the value types such as Int, Double, Long, etc, cannot be null, the null in Scala is analogous to the null in Java. Null: It is a Trait, which is a subset of each of the reference types but is not at all a sub-type of value types and a single instance of Null is null .

Null, null, Nil, Nothing, None, and Unit in Scala – Experiences , As the Scala official doc says, Nothing is a subtype of all types. be Int . In the same way, it should be legitimate to use throw where any possible type is required. Author): Future[Unit] = ???def updateAuthorName(authorId: AuthorId, name:  Unit: In Scala, Unit is used to represent “No value” or “No Useful value”. Unit is a final class defined in “scala” package that is “scala.Unit”. Java’s void does not any value. It is nothing. Scala’s Unit has one value () is the one and only value of type Unit in Scala. However, there are no values of type void in Java.

Do you have usages of Unit / Null / Nothing as something else than a return type?


Unit can be used like this:

def execute(code: => Unit):Unit = {
  // do something before
  code
  // do something after
}

This allows you to pass in an arbitrary block of code to be executed.


Null might be used as a bottom type for any value that is nullable. An example is this:

implicit def zeroNull[B >: Null] =
    new Zero[B] { def apply = null }

Nothing is used in the definition of None

object None extends Option[Nothing]

This allows you to assign a None to any type of Option because Nothing 'extends' everything.

val x:Option[String] = None

What is Scala's Nothing type for? - jooohn, Scala Terminologies: Null, null, Nil, Nothing, None, Unit. We can use Nothing as a return type of methods which never return. We can use  AnyRef is the supertype of all the reference classes (like String, List, Iterable) in scala. AnyVal is the supertype of all the value classes (like Int, Float, Double, Byte, Short..). Null is a subtype of all the reference classes. null is its only instance. Nothing is subtype of every other type i.e of reference and value classes.

if you use Nothing, there is no things to do (include print console) if you do something, use output type Unit

object Run extends App {
  //def sayHello(): Nothing = println("hello?")
  def sayHello(): Unit = println("hello?")
  sayHello()
}

... then how to use Nothing?

trait Option[E]
case class Some[E](value: E) extends Option[E]
case object None extends Option[Nothing]

Null, null, Nil, Nothing, None, and Unit in Scala – Harshit Jain, Representing Emptiness In Scala (with Null, null, Nothing, Nil, None, and Unit). 31 Mar 2013. There are a couple of explanations of these terms out there already​  AnyRef is the supertype of all the reference classes (like String, List, Iterable) in scala. AnyVal is the supertype of all the value classes (like Int, Float, Double, Byte, Short..). Null is a subtype of all the reference classes. null is its only instance. Nothing is subtype of every other type i.e of reference and value classes.

I've never actually used the Null type, but you use Unit, where you would on java use void. Nothing is a special type, because as Nathan already mentioned, there can be no instance of Nothing. 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 to Nil - which is a List[Nothing] - and the list will then be of this elements type. None also if of type Option[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 type Nothing.

Representing Emptiness In Scala (with Null, null, Nothing, Nil, None , One of the usages of Nothing is that it signals abnormal termination. Any time you feel like using a null , use an Option instead. Next » · « Previous. Null, null, Nil, Nothing, None, and Unit in Scala Null - is a subtype of all reference types; its only instance is the null reference. Since Null is not a subtype of value types, null is not a member of any such type. null - Is an only instance of Null.

Scala Tutorial - Scala Nothing and Null Types, There are nine predefined value types and they are non-nullable: Double , Float , Long , Int All functions must return something so sometimes Unit is a useful return type. Nothing is a subtype of all types, also called the bottom type. A common use is to signal non-termination such as a thrown exception, program exit,  None is the counterpart to Some, used when you’re using Scala’s Option class to help avoid null references. If you’re not familiar with the idea of Option or Maybe, here’s an introduction to Option. Odds are you'll see this one. Unit. Unit in Scala is the equivalent of void in Java. It’s used in a function’s signature when that function doesn’t return a value.

Unified Types | Tour of Scala, Nothing is - together with scala.Null - at the bottom of Scala's type hierarchy. Another usage for Nothing is the return type for methods which never return  Nothing is - together with Null - at the bottom of Scala's type hierarchy. Nothing is a subtype of every other type (including Null ); there exist no instances of this type. Although type Nothing is uninhabited, it is nevertheless useful in several ways.

Scala Standard Library 2.13.2, out that Scala includes several notions of nothingness (Null, null, Nil, Nothing, None, and Unit) and that you have to know which one to use in  Unit in Scala is the equivalent of void in Java. It’s used in a function’s signature when that function doesn’t return a value. Odds are you'll see this one. One of the main complaints you hear about the Scala language is that it’s too complicated compared to Java.

Comments
  • Thanks, by "never returns" do you mean that the call is blocking indefinitely (for exemple the startup method of job scheduler?)
  • @Sabastien: it doesn't return normally, it could throw an exception (see james-iry.blogspot.com/2009/08/…). if the blocking call only ends with throwing an exception then that would count. thanks for the question, this needed clarifying.
  • that's true, so in the end there is no other usage of Null?
  • @SebastienLorber edited the answer. I can't actually see any usage for the average developer. Maybe somebody else can think of something useful.
  • Thanks for this. If we know the reason for this kind of things, we understand them, otherwise we remember them.
  • Null is useful when you have a type parameter and may want to return null like in this question and answer, since your discouraged from using null in scala it rarely comes up though, there might be other usages in the type system as well
  • Ok. For your usage of Unit you could have used a generic type so that the execute may return this generic type if your code block returns something else than unit.
  • As @drexin said in a comment on another answer, it's mostly used to denote a side effect.
  • Also, E in the Option has to be in covariant position: trait Option[+E] to allow things like val x: Option[Int] = None
  • thanks I didn't know that None extends Option[Nothing]. It makes sense in some generics usage where a subtype could use Nothing (I guess it's hard to find an exemple for Null and Unit...)
  • Unit is used, where side effects occur, for example the IO monad can be of type IO[Unit] for printing to the console and the like.
  • Yes, never used the IO monad it makes sense to use it with Unit (and perhaps Nothing if it it is some IO operation that produces an infinite stream?)
  • No, Nothing makes no sense there.