为什么在这种情况下隐式转换不起作用?

时间:2018-04-12 11:03:27

标签: scala implicit-conversion

我在Scala中有以下代码。假设有一个A到B的隐式转换。为什么我的隐式List [A]到List [B]转换不起作用?

object ImplicitTest {    
  case class CaseClassA(a: String)
  case class CaseClassB(b: String)

  implicit def a2b(a: CaseClassA): CaseClassB = CaseClassB(a.a)

  implicit def listImplicitConversion[A, B](l: List[A])(implicit conv: A => B): List[B] = l.map(conv)

  def main(args: Array[String]) {
    val listOfA: List[CaseClassA] = List(CaseClassA("a"))
    val listOfB: List[CaseClassB] = listOfA
  }
}

编译此代码会导致以下错误:

Error:(14, 37) type mismatch;
 found   : List[ImplicitTest.CaseClassA]
 required: List[ImplicitTest.CaseClassB]
    val listOfB: List[CaseClassB] = listOfA

1 个答案:

答案 0 :(得分:4)

这是一个错误,即SI-10405,我刚才开过。

由于某种原因,Predef导致隐式搜索失败。如果您使用-Yno-predef标记编译代码,那么您将成功。

症状是您在Predef.conforms[A]下编译时看到typer试图解析隐式的-Xlog-implicits

λ scalac -Xlog-implicits Bar.scala
Bar.scala:16: listImplicitConversion is not a valid implicit value for List[yuval.tests.ImplicitTest.CaseClassA] => List[yuval.tests.ImplicitTest.CaseClassB] because:
incompatible: (l: List[yuval.tests.ImplicitTest.CaseClassA])(implicit conv: yuval.tests.ImplicitTest.CaseClassA => yuval.tests.ImplicitTest.CaseClassA)List[yuval.tests.ImplicitTest.CaseClassA] does not match expected type List[yuval.tests.ImplicitTest.CaseClassA] => List[yuval.tests.ImplicitTest.CaseClassB]
    val listOfB: List[CaseClassB] = listOfA

并忽略任何进一步的搜索,虽然它之前成功匹配:

|-- List[CaseClassB] TYPEmode (site: value listOfB in ImplicitTest)
|    |    |    |    |    |-- scala.`package` EXPRmode-POLYmode-QUALmode (site: value listOfB in ImplicitTest)
|    |    |    |    |    |    \-> scala.type
|    |    |    |    |    |-- CaseClassB TYPEmode (site: value listOfB in ImplicitTest)
|    |    |    |    |    |    \-> yuval.tests.ImplicitTest.CaseClassB
|    |    |    |    |    \-> List[yuval.tests.ImplicitTest.CaseClassB]
|    |    |    |    |-- listOfA : pt=List[yuval.tests.ImplicitTest.CaseClassB] BYVALmode-EXPRmode (site: value listOfB in ImplicitTest)
|    |    |    |    |    [search #2] start `List[yuval.tests.ImplicitTest.CaseClassA]`, searching for adaptation to pt=List[yuval.tests.ImplicitTest.CaseClassA] => List[yuval.tests.ImplicitTest.CaseClassB] (silent: value listOfB in ImplicitTest) implicits disabled
|    |    |    |    |    [search #2] considering listImplicitConversion
|    |    |    |    |    |-- listImplicitConversion BYVALmode-EXPRmode-FUNmode-POLYmode (silent: value listOfB in ImplicitTest) implicits disabled
|    |    |    |    |    |    [adapt] [A, B](l: List[A])(implicit conv: A => B)List[B] adapted to [A, B](l: List[A])(implicit conv: A => B)List[B]
|    |    |    |    |    |    \-> (l: List[A])(implicit conv: A => B)List[B]
|    |    |    |    |    solving for (A: ?A, B: ?B)
|    |    |    |    |    [search #3] start `[A, B](l: List[A])(implicit conv: A => B)List[B]` inferring type B, searching for adaptation to pt=yuval.tests.ImplicitTest.CaseClassA => B (silent: value listOfB in ImplicitTest) implicits disabled
|    |    |    |    |    [search #3] considering a2b
|    |    |    |    |    |-- { ((a: yuval.tests.ImplicitTest.CaseClassA) => ImplicitTe... : pt=yuval.tests.ImplicitTest.CaseClassA => ? EXPRmode (silent: value listOfB in ImplicitTest) implicits disabled
|    |    |    |    |    |    |-- ((a: yuval.tests.ImplicitTest.CaseClassA) => ImplicitTest... : pt=yuval.tests.ImplicitTest.CaseClassA => ? EXPRmode (silent: value listOfB in ImplicitTest) implicits disabled
|    |    |    |    |    |    |    |-- ImplicitTest.this.a2b(a) EXPRmode (silent: value $anonfun in ImplicitTest) implicits disabled
|    |    |    |    |    |    |    |    |-- ImplicitTest.this.a2b BYVALmode-EXPRmode-FUNmode-POLYmode (silent: value $anonfun in ImplicitTest) implicits disabled
|    |    |    |    |    |    |    |    |    \-> (a: yuval.tests.ImplicitTest.CaseClassA)yuval.tests.ImplicitTest.CaseClassB
|    |    |    |    |    |    |    |    |-- a : pt=yuval.tests.ImplicitTest.CaseClassA BYVALmode-EXPRmode (silent: value $anonfun in ImplicitTest) implicits disabled
|    |    |    |    |    |    |    |    |    \-> yuval.tests.ImplicitTest.CaseClassA
|    |    |    |    |    |    |    |    \-> yuval.tests.ImplicitTest.CaseClassB
|    |    |    |    |    |    |    \-> yuval.tests.ImplicitTest.CaseClassA => yuval.tests.ImplicitTest.CaseClassB
|    |    |    |    |    |    \-> yuval.tests.ImplicitTest.CaseClassA => yuval.tests.ImplicitTest.CaseClassB
|    |    |    |    |    [adapt] a2b adapted to { ((a: yuval.tests.ImplicitTest.CaseClassA) => ImplicitTe... based on pt yuval.tests.ImplicitTest.CaseClassA => B
|    |    |    |    |    [search #3] solve tvars=?B, tvars.constr= >: yuval.tests.ImplicitTest.CaseClassB
|    |    |    |    |    solving for (B: ?B)
|    |    |    |    |    [search #3] success inferred value of type yuval.tests.ImplicitTest.CaseClassA => =?yuval.tests.ImplicitTest.CaseClassB is SearchResult({
|    |    |    |    |      ((a: yuval.tests.ImplicitTest.CaseClassA) => ImplicitTest.this.a2b(a))
|    |    |    |    |    }, TreeTypeSubstituter(List(type B),List(yuval.tests.ImplicitTest.CaseClassB)))

如果您对所有输出感到害怕,那么这就是相关部分:

[search #3] success inferred value of type yuval.tests.ImplicitTest.CaseClassA => =?yuval.tests.ImplicitTest.CaseClassB is SearchResult({
  ((a: yuval.tests.ImplicitTest.CaseClassA) => ImplicitTest.this.a2b(a))
}, TreeTypeSubstituter(List(type B),List(yuval.tests.ImplicitTest.CaseClassB)))