这无法使用 Scala 2.10 编译
object TestImplicits {
class View
implicit class RichView[A <: View](v: A) {
def onClicked() = println(v + " was clicked")
}
implicit def intToView[A <: View](i: Int): A = ???
implicit def intToRichView(id: Int): RichView[View] = ???
1.onClicked()
}
错误如下:
<console>:20: error: type mismatch;
found : Int(1)
required: ?{def onClickedd: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method intToView in object TestImplicits of type [A <: TestImplicits.View]
(i: Int)A
and method intToRichView in object TestImplicits of type (id: Int)TestImplicits
.RichView[TestImplicits.View]
are possible conversion functions from Int(1) to ?{def onClickedd: ?}
1.onClickedd()
^
但是等等,View 类没有方法onClicked
!这就是我遇到问题的原因:
- 当我使用它来实现我的第二个函数时,我无法删除第一个隐式函数。
- 如果我删除第二个隐式函数,我将失去电源功能。
我怎样才能同时保留两者?
更新
如果我更改1.onClicked()
为intToView(1).onClicked()
,我会收到以下有趣的错误:
<console>:20: error: value onClicked is not a member of Nothing
intToView(1).onClicked()
^
如果有人可以向我解释这一点,我将不胜感激。