我寫了這個代碼斯卡拉Implicits編譯錯誤
case class Foo(x: Int)
case class Bar(y: Int)
object Foo {
implicit def toBar(f: Foo) : Bar = { Bar(f.x) }
implicit def toBarList(fl: List[Foo]) : List[Bar] = {fl.map{x: Bar => x}}
的toBarList功能不編譯。
<console>:17: error: type mismatch;
found : Bar => Bar
required: Foo => ?
implicit def toBarList(fl : List[Foo]) : List[Bar] = { fl.map{x : Bar => x}}
但是不應該將implicits系統踢在這裏嗎?這意味着編譯器應該檢測到函數期望一個Bar的錯誤,但我傳遞了一個Foo。現在已經有一個隱式函數將Foo轉換爲Bar,所以應該使用它,並且應該可以工作。
那爲什麼不編譯?
在第一個片段,'{fl.map {X:酒吧=> B}}',也許你的意思'{fl.map {X: Bar => x}}'或'{fl.map {b:Bar => b}}'。 – dveim