class Complex(val rp: Double, val ip: Double = 0): def +(other: Complex) = Complex(this.rp + other.rp, this.ip + other.ip) def unary_- = Complex(-rp, -ip) override def toString = var result = rp.toString if (ip != 0.0) result += " + " + ip + "i" result // etc. given Conversion[Double, Complex] = Complex(_) given Conversion[Int, Complex] = Complex(_) object converts extends App { val c1 = Complex(3, 4) val c2 = Complex(5, 9) var c3 = c1 + c2 println(c3) // 8.0 + 13.0i c3 = c1 + 5 println(c3) // 8.0 + 4.0i c3 = 5.1 + c1 println(c3) // 8.1 + 4.0i c3 = Complex(9) println(c3) // 9.0 c3 = -c1 println(c3) // -3.0 + -4.0i }