我在Scala隐式解析中遇到了意外的行为,我想知道该错误是在我的理解还是在Scala编译器中。
这是代码:

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]]
}

编译错误是:
[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]       ^

最佳答案

是的,这是scala编译器中的bug,此版本已在2.11中修复。

关于scala - Scala 2.10.2隐式可能存在的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18902228/

10-12 22:53