私は次のような機能を持っています:
def ifSome[B, _](pairs:(Option[B], B => _)*) {
for((paramOption, setFunc) <- pairs)
for(someParam <- paramOption) setFunc(someParam)
}
そして、次のようなオーバーロードされた関数:
class Foo{
var b=""
def setB(b:String){this.b = b}
def setB(b:Int){this.b = b.toString}
}
val f = new Foo
次に、次の行でエラーが発生します。
ifSome(Option("hi") -> f.setB _)
<console>:11: error: ambiguous reference to overloaded definition,
both method setB in class Foo of type (b: Int)Unit
and method setB in class Foo of type (b: String)Unit
match expected type ?
ifSome(Option("hi") -> f.setB _)
しかし、コンパイラは私たちが Function1[java.lang.String, _] を探していることを認識しています。何か不足していますか、それともコンパイラのバグですか (または、機能のリクエストである可能性があります)?
このような型注釈を使用して、これを回避できました
ifSome(Option("hi") -> (f.setB _:String=>Unit))
しかし、なぜこれが必要なのかを理解したいと思います。