当前,我正在创建一个函数,以功能方式对长整数求和。我的第一次尝试是这样。
def aVeryBigSum(ar: Array[Long]): Long = (0 /: ar) (_ + _)
由于转换不明确而导致错误。
error: type mismatch;
found : x$2.type (with underlying type Int)
required: ?{def +(x$1: ? >: Long): ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method int2long in object Int of type (x: Int)Long
and method int2float in object Int of type (x: Int)Float
are possible conversion functions from x$2.type to ?{def +(x$1: ? >: Long): ?}
def aVeryBigSum(ar: Array[Long]): Long = (0 /: ar) (_ + _)
^
我下一次尝试的工作是显式转换,结果相当冗长,但目前仍在工作。
def aVeryBigSum(ar: Array[Long]): Long =
(0.asInstanceOf[Long] /: ar) (_ + _)
我从Scala开始,所以我认为我可能会误解错误。
还有其他合适的解决方案吗?