Null, null, Nil, Nothing, None, and Unit in Scala

Null– Its a Trait.
null– Its an instance of Null- Similar to Java null.

Nil– Represents an emptry List of anything of zero length. Its not that it refers to nothing but it refers to List which has no contents.

Nothing is a Trait. Its a subtype of everything. But not superclass of anything. There are no instances of Nothing.

None– Used to represent a sensible return value. Just to avoid null pointer exception. Option has exactly 2 subclasses- Some and None. None signifies no result from the method.

Unit– Type of method that doesn’t return a value of anys sort.

Note: Any is supertype of AnyRef and AnyVal. 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.

Think- AnyRef == Object in Java.

The above is a breif summary of a wonderful post by Matt Malone where he has explained all these concepts in depth with examples. Read the blog post here.

Categories: Scala

Tags: , , , , , , ,

10 replies

  1. Glad you enjoyed the article. Thanks for the shout-out.


  2. fun chatting about nothing. Don’t forget the object _scala.xml.Null_. I was stuck for some time, mistaking it for null. Its used in scala.xml.MetaData which in turn is used for xml attributes.

    Scala is fun!


  3. You have written something that is someway wrong :
    “Unit- Type of method that doesn’t return a value of anys sort.”
    Unit is used like void, but is slightly different. As in PF all functions MUST return a value. Unit is a TYPE (sub class of AnyVal) that has only one value : (), and () is the (useless) value that is returned by functions of type Unit. 😉


  4. Here is a link to some comments and clarifications on twitter related to this post


    • Thanks for sharing and also pointing me to the clarification. I have replied to the tweet. I will look into this and update the post as the content is not my original one, instead a summary of the blog post linked towards the end of the post.


    • Clarification for the tweet mentioned in your comment:

      1. From the documentation of Unit:

      /** `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`.

      From what I stated about Unit: It says: return type of the Method that doesn’t return value of any sorts. The intention here is to state that there is no value backed/contained with in it.

      2. This post doesn’t establish any context about how these types are used is because this post is a summary of an interesting post I found (which I have also linked towards the end of my post) where in there are examples which show how these types are used.


  5. Thanks



  1. Scala: The Successor to the Throne « Metaphysical Developer

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: