0 votes
in Scala Constructs by
What is Nothing in Scala? What is Nil in Scala? What is the relationship between Nothing and Nil in Scala?

1 Answer

0 votes
by

In Scala, Nothing is a Type (final class). It is defined at the bottom of the Scala Type System that means it is a subtype of anything in Scala. There are no instances of Nothing. Use Cases of Nothing In Scala:- If Nothing does not have any instances, then when do we use this one in Scala Applications?

Nil is defined using Nothing (See below for example).

None is defined using Nothing.

object None extends Option[Nothing]

We can use Nothing as a return type of methods which never return.

We can use Nothing as a return type of methods which terminates abnormally.

Nil is an object, which is used to represent an empty list. It is defined in “scala.collection.immutable” package as shown below:

object Nil extends List[Nothing]

Example:-

scala> Nil

res5: scala.collection.immutable.Nil.type = List()

scala> Nil.length

res6: Int = 0

Related questions

0 votes
asked Sep 10, 2022 in Scala Constructs by Robin
0 votes
asked Sep 12, 2022 in Scala Constructs by Robin
...