質問

多くのテクニックを試しましたが、ぶつかっています

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

何らかの方法で。例として、問題を再現する2つの関数を次に示します。 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を Long 定数にする必要があります:&quot; 0L&quot;:

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