Array(7,8,9) map (x:Int=>x+1) //1).error, identifier expected but integer literal found.
Array(7,8,9) map {x:Int=>x+1} //2) correct
Array(7,8,9) map ((x:Int)=>x+1) //3) correct
Array(7,8,9) map (x=>x+1) //4 correct
Array(7,8,9) map {x=>x+1} //5 correct
Array(7,8,9) map x=>x+1 //6 error
Run Code Online (Sandbox Code Playgroud)
我会问上述情况?为什么有些工作而其他人则不像评论所示
为了:
Array(7,8,9) map {x:Int=>x+1} //2) correct
Array(7,8,9) map {x=>x+1} //5 correct
Run Code Online (Sandbox Code Playgroud)
对于单个无类型形式参数,(x) => e 可以缩写为 x => e。如果带有单个类型参数的匿名函数 (x: T) => e 作为块的结果表达式出现,则可以缩写为 x: T => e。
对于 type Int,Scala可以在此上下文下推断出该类型。