Why javaBigDecimal2bigDecimal implicit is not applicable?

implicit-conversion, scala

Solution

In first expression `BigDecimal(1) + new java.math.BigDecimal("1")` works rule:

Compiler will look for implicits defined within any object of the implicit scope of the type it's looking for.

So, there is a method `+(BigDecimal): BigDecimal` on `scala.math.BigDecimal`. Compiler sees wrong argument type (`java.math.BigDecimal`) and starts to look for conversion to type `BigDecimal`. It can't find one in scope, and then it looks in `BigDecimal` object and finds `javaBigDecimal2bigDecimal`.

Second example will work if there is `javaBigDecimal2bigDecimal` conversion in scope because `java.math.BigDecimal` doesn't have `+` method, and compiler will look for conversion to proper type (which has method `+(BigDecimal)`)

Problem

With scala 2.9.2 this code: ``` BigDecimal(1) + new java.math.BigDecimal("1") new java.math.BigDecimal("1") + BigDecimal(1) ``` does not compile because scala.math.BigDecimal$#javaBigDecimal2bigDecimal is not applied in the second case However, if i define the same implicit right before it, the code compiles: ``` BigDecimal(1) + new java.math.BigDecimal("1") implicit def javaBigDecimal2bigDecimal(x: java.math.BigDecimal): BigDecimal = BigDecimal(x) new java.math.BigDecimal("1") + BigDecimal(1) ``` Why so?

Original source