1
在下面的例子:意外的隱式轉換
import scala.language.implicitConversions
class Fraction(val num: Int, val den: Int) {
def *(other: Fraction) = new Fraction(num * other.num, den * other.den)
}
implicit def int2Fraction(n: Int) = new Fraction(n, 1)
implicit def fraction2Double(f: Fraction) = f.num * 1.0/f.den
爲什麼結果的Double
而不是Fraction
?換句話說 - 爲什麼在這裏應用fraction2Double
方法而不是int2Fraction
?
scala> 4 * new Fraction(1, 2)
res0: Double = 2.0