Why do I need parentheses around x:Int in this case:
List(1,2,3,4,5).filter((x:Int) => x > 3)
but not x in this case:
List(1,2,3,4,5).filter(x => x > 3)
If I try:
List(1,2,3,4,5).filter(x:Int => x > 3)
I get:
identifier expected but integer literal found
What exactly does that mean?
The parentheses show where the parameter's type ends. Since the => symbol is valid in scala types (indicating a function type), just having the => there doesn't mean that the type is over. Consider this:
List(Map(1->2)).filter((x: Int => Int) => x(1) == 2)
The parentheses clearly show that the first => is part of the type of x and the second is defining the function.
In your second example, there is no type on x, so there's no ambiguity about the role of the =>.
x: Int => Int - x is function of type Int => Int
(x:Int) => ... - x is function parameter
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