Why does this wildcarded function tells me it has the wrong number of parameters?

后端 未结 2 1773
孤城傲影
孤城傲影 2021-01-15 02:49

The offending code was:

:47: error: wrong number of parameters; expected = 2
            terms.foldLeft(r.unitA)(r.add(_, _.eval(x)))
<         


        
相关标签:
2条回答
  • 2021-01-15 03:15

    From what I've read on this type of issue, 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. So when you wrapped your two placeholders with r.add(), the scope of the params is lost. Check out this link and see if it helps explain the rules better.

    http://www.scala-lang.org/node/2916

    0 讨论(0)
  • 2021-01-15 03:29

    Here is the section of the SLS 6.23:

    http://iainmcgin.github.io/scala-ref-markdown/#placeholder-syntax-for-anonymous-functions

    Updated link:

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题