14

I am learning Scala and am a bit confounded by the difference between next types: 'Null', 'Nil' and 'Nothing'.

Can someone please help explain the difference to me? From what i gather, "Nil" is used to describe an empty list.

6
  • 8
    Possible duplicate of Usages of Null / Nothing / Unit in Scala Commented Oct 17, 2017 at 17:42
  • Please refer to scala api as you first point of reference: scala-lang.org/api/current Commented Oct 17, 2017 at 17:44
  • @Alex L, thank you for the link. But I am still confused as to what Nothing is exactly used for. And I guess how does it differentiate with None which according to the API represents non-existent values?.... Also in the linked thread, the Curcyu pointed out that Nothing is used when the function does nothing at all... I don't really get the whole point of all the special subcategories of 'nothingness' and such. Commented Oct 17, 2017 at 17:54
  • broadly, your question is a duplicate of stackoverflow.com/questions/16173477/…. if you have specific questions or doubts about specifics, consider opening one or more new questions about those specifics Commented Oct 17, 2017 at 18:29
  • Just like Seth said - make your question more specific to what exactly you struggle to understand. Put your comment into a question. Commented Oct 17, 2017 at 18:55

2 Answers 2

18

Nothingness in Scala

There are 7 cases where you want to represent the concept of nothingness in Scala.

Nil - Used for representing empty lists, or collections of zero length. For sets you can use Set.empty

None - One of two subclasses of the Optional type, the other being Some. Very well supported by the Scala collections.

Unit - Equivalent to Java's void which is used for functions that don't have a return type

Nothing - A trait. It is a subtype of all other types, but supertype of nothing. It is like the leaf of a tree. No instances of Nothing.

Null - A trait. Not recommended to be used.

null - An instance of the trait, similarly used as the Java Null. Not recommended to be used.

Best Practices

  • If you have code that is returning null, change it to return an Optional.
  • If you need to have an uninitialized variable, using Optional, or an empty list/collection instead of null
  • *

For best practices recommendation, please see https://alvinalexander.com/scala/scala-null-values-option-uninitialized-variables

Why is null so hated?

Sign up to request clarification or add additional context in comments.

Comments

0

In scala null is realy bad, you must use Optional[T].

We have Nil which is a empty list to be able to do 1 :: Nil to create a list that contain only 1.

And, Nothing is a subtype of every type. In other world, is a object that represent null.

3 Comments

Nothing doesn't 'represent' null, there are no values of type Nothing.
Yes you're right, it's in the doc! Sorry for that. I mean I've never used it in Scala...
There's no Optional[T] in scala its Option[T]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.