top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Nothing in Scala? What is Nil in Scala? What is the relationship between Nothing and Nil in Scala?

0 votes
587 views
What is Nothing in Scala? What is Nil in Scala? What is the relationship between Nothing and Nil in Scala?
posted Jul 13, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

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?
1.Nil is defined using Nothing.
2.None is defined using Nothing.
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]

and here is example for 1,Nil is defined using Nothing

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

scala> Nil.length
res6: Int = 0
answer Jul 17, 2016 by Shivam Kumar Pandey
...