[英]curly braces and parentheses for scala map
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
我想问一下以上情况,为什么有些工作没有注释所表明的
对于:
Array(7,8,9) map {x:Int=>x+1} //2) correct
Array(7,8,9) map {x=>x+1} //5 correct
在单个无类型形式参数的情况下,(x)=> e可以缩写为x => e。 如果带有单个类型参数的匿名函数(x:T)=> e出现在块的结果表达式中,则可以将其缩写为x:T => e。
对于Int
类型, Scala可以在此上下文下推断此Type 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.