Say you have the following:
foo(x: String)(y: Int): Int
foo(x: String)(y: Double): Int
Scala does not allow such expression. As far as I can see, the reason for this is that foo(“asdf”) does not have a well defined type (it’s either Int => Int or Double => Int).
Is there a reason why such “polytyped” functions should not be allowed?
Overloading resolution in Scala takes only the first parameter list into account. That’s why alternatives must differ already in this list. There’s a good reason for this: We can then use the resolved function’s type to infer the type of subsequent arguments. This enables idioms like:
Note that here we need to know the type of
correspondsin order to infer the types ofxandy. It would be a shame to have this break down whencorrespondsis overloaded.