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)