I use the following code written in Scala 2.11.8:
sealed trait Acceptable[T] object Acceptable { implicit object Int extends Acceptable[Int] implicit object String extends Acceptable[String] } case class Enc[T](f: T => Any) implicit def test[I, T](implicit f: I => T, a: Acceptable[T]): Enc[I] = Enc[I](f) val e = implicitly[Enc[Int]]
It compiles successfully.
As you can see, the parameter a: Acceptable[T] should be easily converted to a context binding :
implicit def test[I, T: Acceptable](implicit f: I => T): Enc[I] = Enc[I](f)
But after that, the compilation of the changes fails with an error:
could not find implicit value for parameter e: app.Enc [Int]
Why is this happening?
UPDATE:
I tried the -Xlog-implicits option of the compiler, and the compilation log gives me:
[info] /path/to/ScalaTest/src/main/scala/app/Main.scala:60: test is not a valid implicit value for app.Enc[Int] because: [info] hasMatchingSymbol reported error: ambiguous implicit values: [info] both object Int in object Acceptable of type app.Acceptable.Int.type [info] and object String in object Acceptable of type app.Acceptable.String.type [info] match expected type app.Acceptable[T] [info] val e = implicitly[Enc[Int]] [info] ^ [info] /path/to/ScalaTest/src/main/scala/app/Main.scala:60: app.test is not a valid implicit value for app.Enc[Int] because: [info] hasMatchingSymbol reported error: ambiguous implicit values: [info] both object Int in object Acceptable of type app.Acceptable.Int.type [info] and object String in object Acceptable of type app.Acceptable.String.type [info] match expected type app.Acceptable[T] [info] val e = implicitly[Enc[Int]] [info] ^ [error] /path/to/ScalaTest/src/main/scala/app/Main.scala:60: could not find implicit value for parameter e: app.Enc[Int] [error] val e = implicitly[Enc[Int]]
Ok, I understand this conclusion. But why does this work in the case of an implicit parameter?
mixel source share