gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:25:52 25 4
gpt4 key购买 nike

考虑以下示例:

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 不会即时产生错误并显示隐式用于转换:
Screenshot from Intellij Idea

为了解决这个问题,我使用了这里描述的方法:
"Scala: Making implicit conversion A->B work for Option[A] -> Option[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
}
}

此代码编译良好并按需要工作。

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

最佳答案

Why the first approach fails to compile?



I've opened a bug for this issue .

这似乎是隐式搜索中的编译器怪癖。由于您提供的是 convert转换方法 Seq[A] => Seq[B] ,编译器无法正确对齐类型。这是用 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]

Is this somehow related to type erasure and if yes, how usage of Functor helps with it?



第二个示例有效,因为您现在明确地布置了如何映射 Seq[A]Seq[B]通过使用 Functor类型类,因此当编译器看到 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 s 将它们转换为 B s,这就是它使用 conversions.aToB 所做的事情.

关于容器嵌套类型的Scala隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com