我试图在使用' foreach'迭代收集时使用curried函数。方法:
object CurriedTest {
def main(args: Array[String]): Unit = {
fun("one")("two")("three")
(fun2)("three")
val lst = List ("x", "y", "z")
lst.foreach(fun2)
lst.foreach(fun("one"),("two") _)
}
def fun (a1: String) (a2:String) (a3: String) = {
println("a1: "+a1+" a2: "+a2+" a3: "+a3)
}
def fun2 = fun("one")("two") _
}
为什么行' lst.foreach(有趣("一个"),("两个")_)'不编译并返回:
- too many arguments for method foreach: (f: String => B)Unit
错误消息?
答案 0 :(得分:4)
从此行删除逗号
lst.foreach(fun("one"),("two") _)