Scala 下划线 - 错误:扩展函数缺少参数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7627117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala underscore - ERROR: missing parameter type for expanded function
提问by Jeff Storey
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 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?
有人可以解释为什么第二个语句不能编译吗?
回答by retronym
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.
匿名函数的占位符语法用函数替换了尽可能小的包含表达式。

