It would be great if someone could shed some light on the following REPL sessions (2.10.0) [1]:
They are two attempts to produce a situation with ambiguous implicits. The first works as expected:
trait Settings { def title: String }
implicit object DefaultSettings extends Settings { val title = "default" }
scala> println(implicitly[Settings].title)
default
implicit object TestSettings extends Settings { val title = "test" }
scala> println(implicitly[Settings].title)
<console>:11: error: ambiguous implicit values:
The second attempt - to reproduce the result using an existing trait, Numeric[Int] - is where it gets interesting:
// using default Numeric[Int]: IntIsIntegral
scala> implicitly[Numeric[Int]].times(3, 4)
res0: Int = 12
implicit object StrangeMath1 extends Numeric[Int] {
| def times(x: Int,y: Int): Int = x + y
| // ignore the remainder
| ...
| }
scala> implicitly[Numeric[Int]].times(3, 4)
res1: Int = 7
OK, so no ambiguous implicits so far? Guess
StrangeMath1 overrides
IntIsIntegral somehow? To continue:
implicit object StrangeMath2 extends Numeric[Int] {
| def times(x: Int,y: Int): Int = x + y
| // ignore the remainder
| ...
| }
scala> implicitly[Numeric[Int]].times(3, 4)
res2: Int = 12
OK, so now back to IntIsIntegral?!? Indeed:
scala> println(implicitly[Numeric[Int]])
scala.math.Numeric$IntIsIntegral$@129f8ab4
But then:
scala> implicitly[Numeric[Int]]
<console>:10: error: ambiguous implicit values:
both object StrangeMath1 of type StrangeMath1.type
and object StrangeMath2 of type StrangeMath2.type
match expected type Numeric[Int]
implicitly[Numeric[Int]]
A quick search through the issue list and otherwise online didn't turn up anything that appears to discuss this directly. Thoughts/pointers as to what might be going on here much appreciated!
Regards
ap