【发布时间】:2016-12-29 18:11:37
【问题描述】:
我已经阅读了stackoverflow中的几个大括号和大括号的差异,例如What is the formal difference in Scala between braces and parentheses, and when should they be used?,但我没有找到我以下问题的答案
object Test {
def main(args: Array[String]) {
val m = Map("foo" -> 3, "bar" -> 4)
val m2 = m.map(x => {
val y = x._2 + 1
"(" + y.toString + ")"
})
// The following DOES NOT work
// m.map(x =>
// val y = x._2 + 1
// "(" + y.toString + ")"
// )
println(m2)
// The following works
// If you explain {} as a block, and inside the block is a function
// m.map will take a function, how does this function take 2 lines?
val m3 = m.map { x =>
val y = x._2 + 2 // this line
"(" + y.toString + ")" // and this line they both belong to the same function
}
println(m3)
}
}
【问题讨论】:
标签: scala