I have created a simple hierarchy with implicit coversions to achieve diamond during implicit resolution. Scalac fails compilation with the following advice:
Note that implicit conversions are not applicable because they are ambiguous:
[error] both method toMessage in object B of type (b: fiddles.implicits.ImplicitResolutionDiamond.B)Array[String]
[error] and method toMessage in object A of type (a: fiddles.implicits.ImplicitResolutionDiamond.A)Array[String]
[error] are possible conversion functions from fiddles.implicits.ImplicitResolutionDiamond.C to Array[String]
When I add Clippy sbt plugin and set clippyColorsEnabled := true this information is lost and I get only this one instead:
Clippy advises, pay attention to the marked parts:
[error] found : fiddles.implicits.ImplicitResolutionDiamond.C
[error] required: Array[String]
The test code.
CAUTION: Don't use such invisible implicit conversions in real code. They are designed as quick example to fail implicit search during compilation due to ambiguity:
object ImplicitResolutionDiamond {
trait A
object A {
implicit def toMessage(a: A): Array[String] = {
println("Implicit conversion A -> Array[String]")
Array("A", "Hello World")
}
}
trait B
object B {
implicit def toMessage(b: B): Array[String] = {
println("Implicit conversion B -> Array[String]")
Array("B", "Hello World")
}
}
// Companion objects of all C supertypes will be searched for implicits.
class C extends A with B
def show(message: Array[String]): Unit = {
println(s"Shown: ${message.mkString(",")}")
}
def run(): Unit = {
val c = new C
println("Called show with C, show requires Array[String] which triggers implicit conversion")
show(c)
}
}
I have created a simple hierarchy with implicit coversions to achieve diamond during implicit resolution. Scalac fails compilation with the following advice:
When I add Clippy sbt plugin and set
clippyColorsEnabled := true
this information is lost and I get only this one instead:The test code. CAUTION: Don't use such invisible implicit conversions in real code. They are designed as quick example to fail implicit search during compilation due to ambiguity: