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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.