{ case => ...}被称为case序列(见《Scala编程》中文版之“作为偏函数的case序列”),它可以出现在任何允许函数字面量出现的地方。而*.map { case => ...}省略了*.map()里的(),见下面例子
def main(args: Array[String]): Unit = {
def call(method : Int => Int): Unit = {
method(1)
}
call x => x+1 //error
call(x => x+1) //ok
call({x => x+1}) //ok
call{x => x+1} //ok
}
原因是,scala允许代码块作为参数时,省略()。见syntax - What is the formal difference in Scala between braces and parentheses, and when should they be used? - Stack Overflow
本文探讨了Scala编程中case序列的用法,它可以在函数字面量出现的任何地方使用。文章通过示例展示了如何使用case=>...简化.map操作,并解释了在Scala中代码块作为参数时可以省略括号的语法特性。
5530

被折叠的 条评论
为什么被折叠?



