【发布时间】:2014-03-31 21:31:13
【问题描述】:
为什么iWantInt(a) 无法编译,而iWantInt(b) - 更令人惊讶的是 - iWantInt 可以编译?
我如何理解这种行为?
当我可以隐式传递时,为什么我不能显式传递a 到iWantInt?
iWantA 与 iWantInt 有何不同?为什么它的工作方式不同?
这种行为是否在某处记录/解释过?
如果 Scala 接受 iWantInt(a) 会导致什么样的问题?为什么会被禁止?
class A
class B
object Run extends App {
implicit val a = new A
implicit val b = new B
implicit def A2Int(implicit a:A)=1
implicit def B2Int(b:B)=2
def iWantA(implicit a:A)=println("A")
def iWantInt(implicit i: Int) = println(i)
iWantInt(b) // prints 2
iWantInt // prints 1
// iWantInt(a) // when uncommented this line does not compile, why ?
iWantA // prints A
iWantA(a) // prints A
}
【问题讨论】:
标签: scala implicit-conversion implicit