容器嵌套类型的 Scala 隐式转换



请考虑以下示例:

case class A()
case class B()
object Conversions {
implicit def aToB(a: A): B = B()
implicit def convert[U, T](seq: Seq[U])(implicit converter: U => T): Seq[T] = {
seq.map(converter)
}
}
object Main {
import Conversions._
def main(args: Array[String]): Unit = {
val sa = Seq(A())
def example(): Seq[B] = sa
}
}

此示例不会由版本 2.11.8 的 scala 编译器编译。我使用 IntelliJ Idea 进行编译,但实际上 idea 不会即时产生错误,并表明隐式用于转换: 截图来自Intellij Idea
为了解决此问题,我使用了此处描述的方法: "Scala:使隐式转换 A->B 适用于选项[A] ->选项[B]"
我的代码开始如下所示:

case class A()
case class B()
object Conversions {
implicit def aToB(a: A): B = B()
trait ContainerFunctor[Container[_]] {
def map[A, B](container: Container[A], f: A => B): Container[B]
}
implicit object SeqFunctor extends ContainerFunctor[Seq] {
override def map[A, B](container: Seq[A], f: (A) => B): Seq[B] = {
Option(container).map(_.map(f)).getOrElse(Seq.empty[B])
}
}
implicit def functorConvert[F[_], A, B](x: F[A])(implicit f: A => B, functor: ContainerFunctor[F]): F[B] = functor.map(x, f)
}
object Main {
import Conversions._
def main(args: Array[String]): Unit = {
val sa = Seq(A())
def example(): Seq[B] = sa
}
}

此代码编译良好,并根据需要工作。

我的问题是:
为什么第一种方法无法编译?
这是否与类型擦除有关,如果是,使用函子如何帮助它?
编译器如何解析这两种情况的隐式?

为什么第一种方法无法编译?

我已经为这个问题打开了一个错误。

这似乎是隐式搜索中的编译器怪癖。由于您提供了转换Seq[A] => Seq[B]convert方法,因此编译器无法正确对齐类型。这是使用Ytyper-debug编译的输出:

|    [search #3] start `[U, T](seq: Seq[U])(implicit converter: U => T)Seq[T]` inferring type T, searching for adaptation to pt=A => T (silent: method example in Test) implicits disabled
|    [search #3] considering aToB
|    |-- { ((a: A) => Conversions.aToB(a)) } : pt=A => ? EXPRmode (silent: method example in Test) implicits disabled
|    |    |-- ((a: A) => Conversions.aToB(a)) : pt=A => ? EXPRmode (silent: method example in Test) implicits disabled
|    |    |    |-- Conversions.aToB(a) EXPRmode (silent: value $anonfun in Test) implicits disabled
|    |    |    |    |-- Conversions.aToB BYVALmode-EXPRmode-FUNmode-POLYmode (silent: value $anonfun in Test) implicits disabled
|    |    |    |    |    -> (a: A)B
|    |    |    |    |-- a : pt=A BYVALmode-EXPRmode (silent: value $anonfun in Test) implicits disabled
|    |    |    |    |    -> A
|    |    |    |    -> B
|    |    |    -> A => B
|    |    -> A => B
|    [adapt] aToB adapted to { ((a: A) => Conversions.aToB(a)) } based on pt A => T
|    [search #3] solve tvars=?T, tvars.constr= >: B
|    solving for (T: ?T)
|    [search #3] success inferred value of type A => =?B is SearchResult({
|      ((a: A) => Conversions.aToB(a))
|    }, TreeTypeSubstituter(List(type T),List(B)))
|    solving for (A: ?A)
|    solving for (A: ?A)
|    solving for (A: ?A)
|    solving for (A: ?A)
|    [search #3] considering $conforms
|    solving for (A: ?A)
|    [adapt] $conforms adapted to [A]=> <:<[A,A] based on pt A => T
|    [search #3] solve tvars=?T, tvars.constr= >: A
|    solving for (T: ?T)
|    [search #3] success inferred value of type A => =?A is SearchResult(scala.Predef.$conforms[A], TreeTypeSubstituter(List(type T),List(A)))

似乎搜索 #3 正在尝试适应conforms(<:<(,它将整个隐式搜索从A => B带到A => A。如果我使用-Yno-predef进行编译,隐式转换成功:

|    |    |-- [U, T](seq: Seq[U])(implicit converter: U => T)Seq[T] : pt=Seq[B] EXPRmode (silent: method example in Test) implicits disabled
|    |    |    [search #4] start `[U, T](seq: Seq[U])(implicit converter: U => T)Seq[T]`, searching for adaptation to pt=A => B (silent: method example in Test) implicits disabled
|    |    |    [search #4] considering aToB
|    |    |    |-- { ((a: A) => Conversions.aToB(a)) } : pt=A => B EXPRmode (silent: method example in Test) implicits disabled
|    |    |    |    |-- ((a: A) => Conversions.aToB(a)) : pt=A => B EXPRmode (silent: method example in Test) implicits disabled
|    |    |    |    |    |-- Conversions.aToB(a) : pt=B EXPRmode (silent: value $anonfun in Test) implicits disabled
|    |    |    |    |    |    |-- Conversions.aToB BYVALmode-EXPRmode-FUNmode-POLYmode (silent: value $anonfun in Test) implicits disabled
|    |    |    |    |    |    |    -> (a: A)B
|    |    |    |    |    |    |-- a : pt=A BYVALmode-EXPRmode (silent: value $anonfun in Test) implicits disabled
|    |    |    |    |    |    |    -> A
|    |    |    |    |    |    -> B
|    |    |    |    |    -> A => B
|    |    |    |    -> A => B
|    |    |    [adapt] aToB adapted to { ((a: A) => Conversions.aToB(a)) } based on pt A => B
|    |    |    [search #4] success inferred value of type A => B is SearchResult({
|    |    |      ((a: A) => Conversions.aToB(a))
|    |    |    }, )
|    |    |    |-- [U, T](seq: Seq[U])(implicit converter: U => T)Seq[T] : pt=Seq[B] EXPRmode (silent: method example in Test) implicits disabled
|    |    |    |    -> Seq[B]
|    |    |    [adapt] [U, T](seq: Seq[U])(implicit converter: U => T)Seq[T] adapted to [U, T](seq: Seq[U])(implicit converter: U => T)Seq[T] based on pt Seq[B]
|    |    |    -> Seq[B]
|    |    [adapt] Seq[A] adapted to [U, T](seq: Seq[U])(implicit converter: U => T)Seq[T] based on pt Seq[B]
|    |    -> Seq[B]
|    -> [def example] ()Seq[B]

这是否与类型擦除有某种关系,如果是,如何使用 函子帮忙吗?

第二个示例之所以有效,是因为您现在显式地列出了如何使用Functor类型类将Seq[A]映射到Seq[B],因此当编译器看到Seq[A]时,它有一个隐式将其转换为Seq[B]

def example(): Seq[B] = Conversions.functorConvert[Seq, A, B](sa)({
((a: A) => Conversions.aToB(a))
}, Conversions.SeqFunctor);

请注意,您需要从A => B转换,以及一个Functor[Seq]才能映射所有A以将它们转换为Bs,这就是它使用conversions.aToB所做的。

相关内容

  • 没有找到相关文章

最新更新