title: Scala: Return head of list but empty list can't return Nil
body: <p>I'm just starting to learn Scala and i'm having a bit of trouble with a head function. I want to return the first element from a list of A elements. But in the case of Nil, I don't know what to return. The function expects A, but as A is abstract and can be anything, i don't know what to return. </p>
  
  <p>When i pass an empty list to my tails function returning Nil works fine. </p>
  
  <pre><code>sealed trait List[+A]
  case object Nil extends List[Nothing]
  case class Cons[+A](head: A, tail: List[A]) extends List[A]
  
  
  object List {
  
           def sum(ints: List[Int]): Int = ints match {
                    case Nil =&gt; 0
                    case Cons(x,xs) =&gt; x + sum(xs)
           }
  
  
           def tail[A](xs: List[A]): List[A] = {
                   xs match {
                     case Cons(_, ys) =&gt; ys
                     case Nil         =&gt; Nil
               }
           }
  
           def head[A](as: List[A]): A = {
                   as match {
                     case Cons(b, _) =&gt; b
                     case Nil         =&gt; Nil
               }
      }
  }
  
  object e31 {
      def main(args: Array[String]): Unit = {
                    val ex3: List[Int] = Cons(1, Cons(2, Nil))
                    val ex2: List[Int] = Nil;
  
                    println(List.sum(ex3)) //3
                    println(List.tail(ex2)) //Nil
                    println(List.tail(ex3)) //cons(2, Nil)
                    //println(List.head(ex3)) //doesn't work
  
      }
  }
  </code></pre>
  
  <p>Any help to understand the problem is greatly appreciated.</p>