I am obviously missing something about them. In Chapter 6, it's said that an implicit method can be used to convert objects automatically. However, in the two simple classes below, I don't see this happening: File test/A.scala:
package test
object A { implicit def convert( x: Int) = { println("convert") new A(x) } }
class A( i : Int ) { val v = i override def toString() = i.toString
def m () = { this }
def + ( r : A) = { i + r.v } }
File: test/B.scala
package test
object B {
def m1() = { val a : A = 6 m2(5 + a) // Compiler error here }
def m2( x : A) = { x.toString }
}
The error is:
test/B.scala:7: error: overloaded method value + with alternatives (Int)Int <and> (Char)Int <and> (Short)Int <and> (Byte)Int cannot be applied to (test.A) m2(5 + a) ^ one error found
From reading Chapter 6 (and ahead some in Chapter 21), I expected that I would be able to invoke the + operator and m() on integers since I have the implicit convert method in the companion object to A.
I think implicit conversion works only if the compiler cannot find the desirable method on the would-be-converted type. So if you change the name of the operation from "+" to say "$" (there's no operation "$" on integers) in class A (and correspondingly in B - "5 $ a"), your code will work (if you also don't forget to include "import A._;" in B).
Or else, if you just swap the values in B - "a + 5" instead of "5 + a", your code will again compile.