Scala underscore - ERROR: missing parameter type for expanded function

只愿长相守 提交于 2019-12-27 13:59:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7627117/scala-underscore-error-missing-parameter-type-for-expanded-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!