scala 查找隱式參數時和進行隱式轉換時的查找順序 (看的一些視頻上老師說的)
當前作用域-->自身作用域伴生對象--> 相關的類和接口的伴生對象(如繼承的類,trait) -->import上下文的隱式轉換
開發時 業界主流一般建議直接在自身伴生對象中寫隱式的, 不建議import,隨着版本的迭代什麼的,某些開發者容易搞混
在哪查找隱式參數時和進行隱式轉換:(看的我腦瓜疼)
https://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html#implicit-scope-of-an-arguments-type
下面的 1 + new A(1) 是可行的 ,找到 object A 中的隱式轉換
class A(val n: Int) {
def +(other: A) = new A(n + other.n)
}
object A {
implicit def fromInt(n: Int) = new A(n)
}
// This becomes possible: because it is converted into this:A.fromInt(1) + new A(1)
1 + new A(1)