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
I would ask for the above cases,why some work while others are not as the comments indicate
For:
Array(7,8,9) map {x:Int=>x+1} //2) correct
Array(7,8,9) map {x=>x+1} //5 correct
From Scala Specification Anonymous Function definition:
In the case of a single untyped formal parameter, (x) => e can be abbreviated to x => e. If an anonymous function (x: T) => e with a single typed parameter appears as the result expression of a block, it can be abbreviated to x: T => e.
and for type Int
, Scala can infer this Type under this context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With