title: scala - why do I encounter this "java.util.NoSuchElementException: head of empty list"
body: <p>Here I have a piece of scala code</p>
  
  <pre><code>abstract class Foo[T] {
    def unit: T
    def add(x:T, y:T): T
  } 
  object Test extends App {
    implicit object StringFoo extends Foo[String] {
      def add(x:String, y:String): String = x concat y
      def unit: String = ""
    }
  
    implicit object IntFoo extends Foo[Int] {
      def add(x:Int, y:Int):Int = x+y
      def unit: Int = 0
    }
  
    def sum[T](xs: List[T])(implicit foo: Foo[T]):T = 
      foo.add(xs.head, sum(xs.tail))
  
  
    println(sum(List(1,2,3)))
    println(sum(List("a","b","c")))
  }
  </code></pre>
  
  <p>After successful compile, I encount a "java.util.NoSuchElementException: head of empty list"</p>
  
  <pre><code>java.util.NoSuchElementException: head of empty list
  at scala.collection.immutable.Nil$.head(List.scala:337)
  at scala.collection.immutable.Nil$.head(List.scala:334)
  at Test$.sum(test.scala:17)
  at Test$.sum(test.scala:17)
  at Test$.sum(test.scala:17)
  at Test$.sum(test.scala:17)
  at Test$delayedInit$body.apply(test.scala:20)
  at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
  at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
  at scala.App$$anonfun$main$1.apply(App.scala:71)
  at scala.App$$anonfun$main$1.apply(App.scala:71)
  at scala.collection.immutable.List.foreach(List.scala:318)
  at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
  at scala.App$class.main(App.scala:71)
  at Test$.main(test.scala:5)
  at Test.main(test.scala)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:71)
  at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
  at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:139)
  at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:71)
  at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:139)
  at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
  at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
  at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
  at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
  at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74)
  at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
  at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
  at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
  </code></pre>
  
  <p>However, the problem disappear if I replace the method <code>sum</code> in this way:</p>
  
  <pre><code>def sum[T](xs: List[T])(implicit foo: Foo[T]: T =
      if(xs.isEmpty) foo.unit
      else foo.add(xs.head, sum(xs.tail))
  </code></pre>
  
  <p>I indeed apply <code>sum</code> with non-empty List, why do I encounter that problem above?</p>