本文介绍了为什么这个通配符函数告诉我它有错误数量的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

违规代码是:

<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

更新链接:

http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#placeholder-syntax-for-anonymous-functions

Daniel Sobral 的帖子说:

Daniel Sobral's post says:

"当您使用_"作为匿名参数的占位符时函数,该函数的作用域是最里面的括号包含它.大部分时间.

更新自旋:我认为规范中的语法解释更容易获得,占位符不会转义封闭的 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.

这篇关于为什么这个通配符函数告诉我它有错误数量的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:30