【发布时间】:2015-08-19 13:43:41
【问题描述】:
给定一个简单的序列:
scala> val a = Seq(1.0,2.0,3.0)
res8: Seq[Double] = List(1.0, 2.0, 3.0)
让我们把它们加起来!
scala> a.reduceLeft{_ + _}
res6: Double = 6.0
但是如何明确参数呢?这是我的尝试:
scala> a.reduceLeft{case(b,c) => b+c}
嗯.. 没有 .. 我们有一个类型不匹配:
<console>:9: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: (?, Double) => ?
a.reduceLeft{case(b,c) => b+c}
^
<console>:9: error: type mismatch;
found : Any
required: String
a.reduceLeft{case(b,c) => b+c
但即使我明确添加类型,它也不起作用:
scala> a.reduceLeft{case(b:Double,c:Double) => b+c}
<console>:9: error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: (?, Double) => ?
a.reduceLeft{case(b:Double,c:Double) => b+c}
那么这里发生了什么?
【问题讨论】: