When an application a.meth(args)
fails, the compiler searches for an implicit view from a
to something that has the method meth
. Any implicit value or method that conforms to A => { def meth(...) }
will do. If one is found, the code is rewritten as view(a).meth(args)
.
Where does it look? First it looks in the current scope, made up of locally defined implicits, and imported implicits.
(There is actually a second phase of the search, in which conversion methods with by-name arguments are considered, but that's not really important to this answer.)
If this fails, it looks in the implicit scope. This consists of the companion objects of the 'parts' of the type that we're searching for. In this case, we're after A => { def meth(...) }
, so we just look at the companion object of A
(and its super types).
In Scala trunk, the implicit scope is extended a tiny bit, to include the companion objects of the types of the arguments. Not sure if that was already in 2.9.1, perhaps a friendly reader will figure that out for me and update this answer.
So 1 + BigInt(2)
is expanded to BigInt.int2bigInt(1) + BigInt(2)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…