In the following code:
trait Foo[T] {
def get: T
}
implicit object FooInt extends Foo[Int] {
override def get = 0
}
implicit object FooString extends Foo[String] {
override def get = "0"
}
def fooImplicitGetter[T](implicit ev: Foo[T]): T = ev.get
val x: Int = fooImplicitGetter // Does not compile; ambiguous implicit values error
implicit val int = 0
implicit val string = ""
def implicitGetter[T](implicit ev: T): T = ev
val y: Int = implicitGetter // Compiles just fine
In the assignment of x, why can't the compiler infer that the type of fooImplicitGetter is Int, and therefore it needs to use the FooInt instance, as it can in the assignment of y? Is there any way of helping it other than fooImplicitGetter[Int], passing FooInt explicitly, etc.? This is under 2.11 if it matter.
EDIT: This appears to be the same issue mentioned here: Inferring type of generic implicit parameter from return type, so I've modified my example to match. I'm also fine with closing it for duplication, unless someone has an answer.