The main usage for Nothing is when specifying the return type for methods which never return normally (e.g. throw an exception when been called).

object Sample
{
  def main(args:Array[String]):Unit =
  {
    val ob = getMyStack[Int](10)
  }
  def getMyStack[T](num:Int) =
  {
    new EmptyMyStack[T]
  }
}

abstract class MyStack[T](size:Int)
{
  def data:T;
}

class EmptyMyStack[T] extends MyStack[T](0)
{
  def data:Nothing = throw new Exception("empty stack");
}

The following video clip overviews this code sample, shows its execution and explains one of the main common cases in which we will use the Nothing type.

Leave a Reply

Your email address will not be published. Required fields are marked *

Update cookies preferences