I have a situation where I have a class with a parameter that should be contravariant. If I have an implicit object defined for both the base class and the subclass, I get an "ambiguous" error where it does not know which implicit object to select. Is there a way to prioritize implicit objects? I have seen examples of priorities for implicit conversions, but never for types. Thanks
trait Coded[-X] { // contravariant
val x: Int
}
def printCode[O1](p1: O1)(implicit ev: Coded[O1]) =
println(ev.x)
class Baseclass
class Subclass extends Baseclass
object TEST {
implicit object code1 extends Coded[Baseclass] { val x = 5 }
implicit object code2 extends Coded[Subclass ] { val x = 6 }
}
import TEST._
printCode(new Subclass) // error: ambiguous
source
share