Scala에서 Long Long에 대한 작업 수행 (특히 목록을 추가하는 방법)

StackOverflow https://stackoverflow.com/questions/1605175

  •  05-07-2019
  •  | 
  •  

문제

여러 기술을 시도했지만 계속 부딪칩니다.

(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)

어떤 식 으로든. 예를 들어, 문제를 재현하는 두 가지 기능이 있습니다. Sumint는 잘 작동합니다 ... 그러나 Sumlong 오류. 나는 그것을 얻지 못한다.

// 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)(_ + _)
도움이 되었습니까?

해결책

0 a를 만들어야합니다 상수 : "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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top