I've just come across a strange problem while trying to overload a function using a partial function :
class Foo {
def bar(pf: PartialFunction[String, Int]): Foo = ???
def bar(zd: Int): Foo = ???
def zed(pf: PartialFunction[String, Int]): Foo = ???
}
...
new Foo()
.bar (0)
.zed { case _ => 1 } // this line is OK
.bar { case _ => 1 } // This line does not compile
I've pasted this code in the REPL, and got a strange error :
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: ?
.bar { case _ => 1 }
^
I've checked on the Internet and find different explanations for this error, but not one talking about overloading. As far as I understand, a PartialFunction is expanded to an anonymous function. So in this case, this would result in something like :
object Test {
new Foo()
.bar (0)
.zed { case _ => 1 }
.bar { (x:String) => x match { case _ => 1 } }
}
But once pasted in the REPL, I get another error :
<console>:17: error: overloaded method value bar with alternatives:
(zd: Int)Foo <and>
(pf: PartialFunction[String,Int])Foo
cannot be applied to (String => Int)
.bar { (x:String) => x match { case _ => 1 } }
^
Which is fine, as there is no signature taking an anonymous function in argument. So what am I missing ? Am I expanding the partial function incorrectly ?
Thank you for any help !
EDIT
I've just found that the problem may come from an ambiguity regarding which method should be called :
object Test {
new Foo()
.bar (0)
.zed { case _ => 1 }
.bar(PartialFunction({case _ => 1})) // This works
}
Secondly, I've found an interesting similar question here