When I do:
def x(i: Int, i2: Int, i3: Int) = i
x(_: Int, _: Int, 3)
Why do I have to specify that the first two arguments are "Int"?
Is there a way to make the compiler infer types, so that I can type:
x(_, _, 3)
When I do:
def x(i: Int, i2: Int, i3: Int) = i
x(_: Int, _: Int, 3)
Why do I have to specify that the first two arguments are "Int"?
Is there a way to make the compiler infer types, so that I can type:
x(_, _, 3)
Short answer:
You always have to be explicit about the parameter types of anonymous functions, unless a function type is expected. "Partial application" is just a syntactical sugar for creating an anonymous function.
Long answer:
First, note that you don't always have to supply type arguments when partially applying x. Consider this example:
def x(i: Int, i1: Int, i2: Int) = i
def g(f: (Int, Int) => Int) = f(1, 2)
// types are inferred!
g(x(_, _, 3)) // evaluates to 1
// again, types are inferred
val f: (Int, Int) => Int = x(_, _, 3)
So clearly it helps to be in a context where a (Int, Int) => Int is expected.
Second, note that you're defining an anonymous function. You could write it more explicitly like this:
(a, b) => x(a, b, 3)
Written this way, it should be more clear that the defining context (i.e., as a lone expression) sets up no expectation for the type of the anonymous function. In the application of g, on the other hand, g's parameter type implies an expectation for a specific function type.
The compiler could, in principle, infer the types of a and b from the type of x, but it does not, as specified in ยง6.23 of the Scala Language Specification (bold-emphasis added):
The anonymous function (x1: T1, ..., xn: Tn) => e maps parameters xi of types Ti to a result given by expression e. [...]
If the expected type of the anonymous function is of the form scala.FunctionN[S1,...,Sn, R], the expected type of e is R and the type Ti of any of the parameters xi can be omitted, in which case Ti = Si is assumed. If the expected type of the anonymous function is some other type, all formal parameter types must be explicitly given, and the expected type of e is undefined.