I'm using 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 successfully compiles.
As you can see a: Acceptable[T]
parameter should be easily converted to context bound:
implicit def test[I, T: Acceptable](implicit f: I => T): Enc[I] =
Enc[I](f)
But after that change compilation is starting to fail with error:
could not find implicit value for parameter e: app.Enc[Int]
Why does this happen?
UPDATE:
I tried -Xlog-implicits
compiler option and 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 output. But why does it work in case of implicit parameter?