tags:

views:

70

answers:

1

I've tried a number of techniques, but I keep bumping into

(fragment of wtf.scala):3: error: overloaded method value + with alternatives
(Int)Int <and> (Char)Int <and> (Short)Int <and> (Byte)Int cannot be applied to (Long)

in one way or another. As an example, here are two functions to reproduce the problem. sumInt works fine... but sumLong errors. I don't get it.

// compiles (and works) fine
def sumInt(list: List[Int]): Int = list.foldLeft(0)(_ + _)

// compile time error. no + define on Long? I don't get it
def sumLong(list: List[Long]): Long = list.foldLeft(0)(_ + _)
+2  A: 

You need to make the 0 a Long constant: "0L":

scala> def sumLong(list: List[Long]): Long = list.foldLeft(0L)(_ + _)
sumLong: (List[Long])Long
scala> scala> sumLong(List(1L, 2L, 3L))
res2: Long = 6
Jim Ferrans
Yeah, that was it. Thanks. I'm struggling to understand the error. Was I saying "find the Int 0 in the list, then add it to a long?" But, that's allowable, right? I see + (arg0 : Long) : Long defined on Int.
trenton