开发者

Scala underscore - ERROR: missing parameter type for expanded function

I know there have been quite a few questions on this, but I've created a simple example that I thought should work,but still does not and I'm not sure I understand why

val myStrings = new Array[String](3)
// do 开发者_JS百科some string initialization

// this works
myStrings.foreach(println(_))


// ERROR: missing parameter type for expanded function
myStrings.foreach(println(_.toString))

Can someone explain why the second statement does not compile?


It expands to:

myStrings.foreach(println(x => x.toString))

You want:

myStrings.foreach(x => println(x.toString))

The placeholder syntax for anonymous functions replaces the smallest possible containing expression with a function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜