I'm reading the book Scala in Depth, chapter 5 about implicits. The author says this on page 102:
The implicit scope used for implicit views is the same as for implicit parameters. But when the compiler is looking for type associations, it uses the type it's attempting to convert from [my emphasis], not the type it's attempting to convert to.
And yet, a few pages later he shows an example, with a complexmath.ComplexNumber
class. You import i
, which is a ComplexNumber
, and call it's *
method, which takes a ComplexNumber
argument.
import complexmath.i
i * 1.0
To convert 1.0 into a ComplexNumber
, this finds an implicit conversion that was defined like so:
package object complexmath {
implicit def realToComplex(r: Double) = new ComplexNumber(r, 0)
val i = ComplexNumber(0, 1)
But that contradicts the first statement, no? It needed to find Double => ComplexNumber
. Why did it look in the complexmath
package, which is part of the implicit scope for ComplexNumber
but not for Double
?
Either source or target works:
So I think that sentence is wrong (?)