Given scala's type inference, I'd expect following not to fail:
scala> def partiallyApplied(x: Int, y: Int, z: Int) = x + y + z
partiallyApplied: (x: Int, y: Int, z: Int)Int
scala> val partialSum = partiallyApplied(2, 3, _)
<console>:11: error: missing parameter type for expanded function ((x$1) => partiallyApplied(2, 3, x$1))
val partialSum = partiallyApplied(2, 3, _)
^
And of course, this works:
scala> val partialSum = partiallyApplied(2, 3, _:Int)
partialSum: Int => Int = <function1>
Is there a reason why type inference does not help with partially applied function in this case?
Scala specification says exactly in which cases parameter types can be omitted and this isn't one of them:
If the expected type of the anonymous function is of the shape
scala.FunctionN[S1,…,Sn, R]
, or can be SAM-converted to such a function type, the typeTi
of a parameterxi
can be omitted, as far asSi
is defined in the expected type, andTi = Si
is assumed. Furthermore, the expected type when type checkinge
isR
.If there is no expected type for the function literal, all formal parameter types
Ti
must be specified explicitly, and the expected type ofe
is undefined. The type of the anonymous function isscala.FunctionN[T1,…,Tn, R]
, whereR
is the packed type ofe
.R
must be equivalent to a type which does not refer to any of the formal parametersxi
.
Could it be changed to work in this specific case as well? Sure. Is the change worth it? Probably not.