0

我正在使用 scala 泛型和类型边界来了解其可能的用例。我对一个场景感到困惑。

假设我有一个特质 Combinable

trait Combinable[T] {
    def combine(other: T): T
}

我想为 Vector[A] 实现一个隐式定义:

implicit def vectorCombinable[A](self: Vector[A]) = new Combinable[Vector[A]] { // note: using scala 2.11, no SAM support
    override def combine(other: Vector[A]): Vector[A] = self ++ other
}

到目前为止一切都很好,如果我将 Vector 替换为 B 类型上限,问题就开始了GenTraversable

implicit def vectorCombinable[A, B <: GenTraversable[A]](self: B): Combinable[B] = new Combinable[B] {
    override def combine(other: B): B = self ++ other
}

我只是希望此方法以 B 类型返回,但self ++ other失败并出现以下编译错误:

GenTraversable[A] 类型的表达式不符合预期的 B 类型

4

2 回答 2

3

你可以这样做:

implicit def vectorCombinable[A, B <: GenTraversableLike[A, B]]
  (self: B with GenTraversable[A])
  (implicit cbf: CanBuildFrom[B, A, B])
: Combinable[B] = new Combinable[B] {
  override def combine(other: B): B = self ++ other
}

首先,您需要B扩展GenTraversableLike,因为scala.collection.???Like类在其签名中包含其元素的类型和序列的完整类型。例如Vector[Int]扩展GenTraversableLike[Int, Vector[Int]]。因此,在类上定义的操作???Like可以使用序列的完整类型。

其次,您需要selfB with GenTraversable[A],因为编译器应该能够从单个签名中找出序列的类型及其元素的类型。

第三,你必须提供一个隐含CanBuildFrom[B, A, B]的,证明你可以用一个序列B中的类型元素构建一个A序列B。这个证明将提供给++方法GenTraversable

毕竟,它工作正常:

scala> List(1,2,3).combine(List(4,5,6))
res0: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> Set(1,2,3).combine(Set(4,5,6))
res1: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)

scala> Map(1 -> "a", 2 -> "b").combine(Map(1 -> "c", 3 -> "d"))
res2: scala.collection.immutable.Map[Int,String] = Map(1 -> c, 2 -> b, 3 -> d)
于 2018-03-09T20:26:44.700 回答
1

基本上,你不能这样做,因为 aGenTraversable[A]没有告诉你任何关于返回类型的具体信息++,尤其是它不能保证它会返回 a B

即使您进行了扩展B <: GenTraversableLike[A, B],您仍然会遇到同样的问题,即++需要一个隐式CanBuildFrom[Blah, Blahh, That]并返回一个That.

为了保证您的方法combine返回相同类型的集合,而不依赖任何外部CanBuildFroms,您可以这样做:

import scala.collection._
import scala.collection.generic.GenericTraversableTemplate
import scala.language.implicitConversions
import scala.language.higherKinds

trait Combinable[T] {
    def combine(other: T): T
}

implicit def genericCombinable
  [A, CC[X] <: 
    GenericTraversableTemplate[X, CC] with 
    GenTraversable[X] with 
    TraversableOnce[X]
  ]
  (c: CC[A])
: Combinable[CC[A]] = {
  new Combinable[CC[A]] {
    override def combine(other: CC[A]): CC[A] = {
      val bldr = c.genericBuilder[A]
      bldr ++= c
      bldr ++= other
      bldr.result
    }
  }
}

现在它可以编译并与标准库中的大多数集合一起使用,因为它们中的大多数都倾向于实现GenericTraversableTemplate

我建议你不要在这上面花太多时间。例如,scala 猫没有费心Monoid为所有可能的类型提供一个通用实例GenTraversable,它们只是MonoidListand Vector(和其他一些类)上实现,而不是为了GenTraversable(如果我错了,请纠正我)。因此,我不会认为这是一件简单的事情。

最后一点:由于隐式转换,编译器应该给你警告,这是正确的。

于 2018-03-09T15:55:13.387 回答