If you run the code below you'll get an ambiguous implicit
error:
class Foo[T,I](val msg: I)
object Foo {
implicit def provide[T]: Foo[T,String] =
new Foo("I came from a place you can't touch so subtyping can't help you")
}
class User
object User {
implicit object userFoo extends Foo[User,Int](42)
}
def fooOf[T,I](U: T)(implicit foo: Foo[T,I]): Foo[T, I] = foo
fooOf(new User).msg //epic fail:
//Error:(232, 7) ambiguous implicit values:
//both object userFoo in object User of type A$A153.this.User.userFoo.type
//and method provide in object Foo of type [T]=> A$A153.this.Foo[T,String]
//match expected type A$A153.this.Foo[A$A153.this.User,I]
//fooOf(new User).msg;//
//^
Normally Scala puts the companion object of type T
in F[T,I]
in priority over F[_]
's. But not in this case since the type I
in both defined places differ.(if they were both String
, Scala would pick the Foo[User,String]
in the User
Companion object)
I can't (or better say don't want to) touch the Foo
companion object to implement LowerPriorityImplicits
subtyping technic and define F[User,I]
instances inside it on higher priority level. what else can I do?