Reputation: 4469
This works: (1 to 5).reduceLeft( _+_ )
but this doesn't: (x:Int,y:Int)=>_+_
<console>:8: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
(x:Int,y:Int)=>_+_
^
<console>:8: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
(x:Int,y:Int)=>_+_
^
It is in way of inconsistent, since in the first case the anonymous function (_+_
) compiled successfully, but failed for the second case.
Is there something I've missed or am mistaken about? Or is it just the syntax definition?
Upvotes: 1
Views: 138
Reputation: 127711
There is no inconsistency. In the first case you're creating anonymous function with two arguments which are then added, that is, something like this:
(1 to 5).reduceLeft((x, y) => x + y)
In the second case, however, you're creating anonymous function which returns another anonymous function with two arguments:
(x: Int, y: Int) => ((a, b) => a + b)
(It seems that you thought that (x: Int, y: Int) => _+_
is the same as (x: Int, y: Int) => x + y
, and this is not so.)
In the first case you used _ + _
in a context where types of its arguments are known (as a parameter of reduceLeft
). In the second case you seem to use it in a context where type of the parameters of the inner function cannot be deduced, and that's exactly what your error is about.
Upvotes: 9