A => B
関数をからに暗黙的に変換したいList[A] => List[B]
。
私は次の暗黙の定義を書きました:
implicit def lift[A, B](f: A => B): List[A] => List[B] = ...
残念ながら、私が次のコードを書くとき、暗黙は適用されません:
val plusOne: (List[Int]) => List[Int] = (x: Int) => (x + 1)
関数に明示的な時間で注釈を付けると、正常に機能します。
なんで?どうすれば修正できますか?
アップデート。問題は無名関数に固有のようです。比較:
@Test
def localLiftingGenerics {
implicit def anyPairToList[X, Y](x: (X, Y)): List[X] => List[Y] = throw new UnsupportedOperationException
val v: List[String] => List[Int] = ("abc", 239)
}
@Test
def localLiftingFuns {
implicit def fun2ListFun[X, Y](f: X => Y): List[X] => List[Y] = throw new UnsupportedOperationException
val v: List[String] => List[Int] = ((x: String) => x.length)
}
最初のものはうまくコンパイルされています。2番目のものはエラーとしてマークされます