Scala 允许你使用下划线来做一个简单的映射。因此,例如,而不是写:
def roleCall(people: String*){
people.toList.map(x => println(x))
}
...我可以改为:
def roleCall(people: String*){
people.toList.map(println(_))
}
但是由于某种原因我不能写:
def greet(people: String*){
// This won't compile!
people.toList.map(println("Hello " + _))
}
相反,我必须写:
def greet(people: String*){
people.toList.map(x => println("Hello " + x))
}
谁能解释为什么?