Why is Scala's type inferencer not able to resolve this?

Posted by Levi Greenspan on Stack Overflow See other posts from Stack Overflow or by Levi Greenspan
Published on 2010-05-20T23:15:37Z Indexed on 2010/05/20 23:20 UTC
Read the original article Hit count: 156

Filed under:

In the code snippet below - why do I have to give a type annotation for Nil?

Welcome to Scala version 2.8.0.RC2 (OpenJDK Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

scala> List(Some(1), Some(2), Some(3), None).foldLeft(Nil)((lst, o) => o match { case Some(i) => i::lst; case None => lst })          
<console>:6: error: type mismatch;
found   : List[Int]
required: object Nil
   List(Some(1), Some(2), Some(3), None).foldLeft(Nil)((lst, o) => o match { case Some(i) => i::lst; case None => lst })
                                                                                              ^

scala> List(Some(1), Some(2), Some(3), None).foldLeft(Nil:List[Int])((lst, o) => o match { case Some(i) => i::lst; case None => lst })
res1: List[Int] = List(3, 2, 1)

© Stack Overflow or respective owner

Related posts about scala