I'm getting unexpected behavior with Scala implicit resolution, and I'd like to know whether the bug is in my understanding or in the Scala compiler. Here's the code:
trait Trait1[A]
implicit def trait1ToList[A](trait1: Trait1[A]): List[A] = ???
trait Trait2[C]
{
implicit def trait2Implicit[A, C <% List[A]]: Trait2[C] = ???
// Compiles, as expected.
implicitly[Trait2[Trait1[Int]]]
}
{
implicit def trait2Pimp[A, C <% List[A]](int: Int): Trait2[C] = ???
// Compiles, as expected.
implicitly[Int => Trait2[Trait1[Int]]]
// Does not compile, which is unexpected.
// This is weird, because the fact the previous line compiles
// implies the implicit conversion is in scope.
2: Trait2[Trait1[Int]]
}
The compilation error is:
[error] /Users/eric/Dropbox/t/2013_q1/billy/src/test/scala/billy/experiments/wideBaseline/testWideBaselineExperiment.scala:56: No implicit view available from Trait1[Int] => List[A].
[error] 2: Trait2[Trait1[Int]]
[error] ^
Yes, this was a bug in the scala compiler that has since been fixed in 2.11.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With