Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible bug with Scala 2.10.2 implicits

Tags:

scala

implicit

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]       ^
like image 491
emchristiansen Avatar asked Nov 11 '22 21:11

emchristiansen


1 Answers

Yes, this was a bug in the scala compiler that has since been fixed in 2.11.

like image 138
Dia Kharrat Avatar answered Nov 15 '22 08:11

Dia Kharrat