让我们考虑这段代码:
class A
object A{
implicit def A2Int(implicit a:A)=1
implicit def A2String(a:A)="Hello"
}
object Run extends App{
implicit val a: A =new A
import A.A2Int
// without this import this code does not compile, why ?
// why is no import needed for A2String then ?
def iWantInt(implicit i:Int)=println(i)
def iWantString(implicit s:String)=println(s)
iWantInt
iWantString(a)
}
它运行并打印:
1
Hello
现在,如果我们注释掉这一行
import A.A2Int
然后我们得到一个编译错误:
注释掉该行后,为什么 Scala 无法找到A.A2String
是否可以找到A.A2Int
?
如何解决这个问题?
谢谢阅读。