Considering the following case:
trait Companion {
implicit def str(a: A): String =
s"${this.getClass.getSimpleName}: %d" format a.n
}
class A(val n: Int)
object A extends Companion {}
class B(val x: Int, y: Int) extends A(y)
object B extends Companion {}
Now compiling the following code will trigger a diverging implicit error:
val b = new B(5, 2)
val s: String = b
println(s)
because both object A and AA are in the default implicit scope of AA. This is clearly defective: the class AA is more 'exact' than trait A, henceforth it's implicit scope should have higher precedence. Unfortunately, because objects cannot inherit from each other, there is no way to declare this.
So my question is: what is the best way to achieve this, without resorting to non-default implicit scope?