为什么此通配符函数告诉我它的参数数量错误? [英] Why does this wildcarded function tells me it has the wrong number of parameters?
问题描述
违规代码为:
<console>:47: error: wrong number of parameters; expected = 2
terms.foldLeft(r.unitA)(r.add(_, _.eval(x)))
我通过写来解决了我的问题:
I solved my problem by writing:
terms.foldLeft(r.unitA)((a,b) => r.add(a, b.eval(x)))
但是我仍然想知道是什么阻止了我的最初尝试?
But I'd still like to know what prevented my initial attempt?
推荐答案
这是SLS 6.23的部分:
Here is the section of the SLS 6.23:
http://iainmcgin.github.io/scala-ref-markdown/#placeholder-syntax-for-anonymous-functions
更新的链接:
Daniel Sobral的帖子说:
Daniel Sobral's post says:
当您使用"_"作为占位符的匿名参数的占位符时 函数,该函数的范围是最里面的括号 包含它. 大部分时间.
"When you use "_" as a place holder for an anonymous parameter of a function, the scope of that function is the innermost parenthesis containing it. Most of the time.
更新自旋:我认为规范中的语法解释更容易理解,占位符不会转义封闭的Expr
.有很多重复的问题.
Updated spin: I think the syntax explanation from the spec is easier to get, that the placeholder doesn't escape an enclosing Expr
. There are various duplicate questions.
这篇关于为什么此通配符函数告诉我它的参数数量错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!