title: Scala List with type parameter
body: <p>I had a problem coding a function called <strong>head</strong>, that basically replace the head elements with another for the List invoking it: </p>
  
  <pre><code>List(1,2,3,4).head(4) // List(4,2,3,4)
  </code></pre>
  
  <p>The code is obviously useless, I was just trying to have fun with Scala. This is the code:</p>
  
  <pre><code>sealed trait List[+A]{
    def tail():List[A]
    def head[A](x:A):List[A]
  }
  
  object Nil extends List[Nothing]{
    def tail() = throw new Exception("Nil couldn't has tail")
    def head[A](x:A): List[A] = List(x)
  }
  
  case class Cons[+A](x :A, xs: List[A]) extends List[A]{
    def tail():List[A] = xs
    def head[A](a:A): List[A] = Cons(a,xs)
  }
  
  object List{
    def apply[A](as:A*):List[A] = {
      if (as.isEmpty) Nil
      else Cons(as.head,apply(as.tail: _*))
    }
  }
  
  Cons(1,Cons(2,Nil)) == List(1,2)
  Cons(1,Cons(2,Cons(3,Cons(4,Nil)))).tail()
  List(1,2,3,4,5,6,7).tail()
  List(1,2,3,4).head(4)
  </code></pre>
  
  <p>It doesn't not compile and I have this error:</p>
  
  <pre><code>Error:(11, 39) type mismatch;
  found   : A$A318.this.List[A(in class Cons)]
  required: A$A318.this.List[A(in method head)]
   def head[A](a:A): List[A] = Cons(a,xs)
  </code></pre>
  
  <p>Could you explain why, please?</p>
  
  <p>Regards.</p>