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.

Tagged with:
 
About The Author

Mohamed Sanaulla

10 Responses to Null, null, Nil, Nothing, None, and Unit in Scala

  1. Matt says:

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

    • i82much says:

      Thanks for this. I got my Nil and null mixed up and caused some confusion with why I couldn’t instantiate some objects as Nil.

  2. [...] order, underscore being used instead of wildcards, asterisks and default values, many kinds of nothing, no static methods (you need to use singleton objects instead) and other minor things. The [...]

  3. egg says:

    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!

  4. DS says:

    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. ;)

    • It was in a way conveying the same information. I didnt wan’t to explain more as I was trying to link a well explained article.

      Thanks a lot for bringing this up. It makes sense to add some more information

  5. Here is a link to some comments and clarifications on twitter related to this post https://twitter.com/itemstate/status/371957550012317696

    • 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.

  6. Arleigh Dickerson says:

    Thanks

Leave a Reply