我想为 BigDecimal 列表编写一个简短的函数求和函数,并尝试使用:
def sum(xs: List[BigDecimal]): BigDecimal = (0 /: xs) (_ + _)
但我收到了这个错误信息:
<console>:7: error: overloaded method value + with alternatives:
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (BigDecimal)
def sum(xs: List[BigDecimal]): BigDecimal = (0 /: xs) (_ + _)
^
如果我改用 Int,则该函数有效。我猜这是因为 BigDecimal 的运算符重载了+
. BigDecimal 有什么好的解决方法?