考虑以下工作自定义Seq

class MySeq[B](val s: Seq[B])
extends Seq[B]
with GenericTraversableTemplate[B, MySeq]
with SeqLike[B, MySeq[B]] {
  override def companion = MySeq

  def iterator = s.iterator

  def apply(i: Int) = s(i)

  def length = s.length

  override def toString = s map { _.toString } mkString("\n")
}
object MySeq extends SeqFactory[MySeq] {
  implicit def canBuildFrom[B]: CanBuildFrom[Coll, B, MySeq[B]] =
       new GenericCanBuildFrom[B]
  def newBuilder[B] = new ListBuffer[B] mapResult (x => new MySeq(x.toSeq))
}


我想对类型参数B施加限制。换句话说,我想要这样的东西(不起作用):

class MyA

class MySeq[+B <: MyA](val s: Seq[B])
extends Seq[B]
with GenericTraversableTemplate[B, MySeq]
with SeqLike[B, MySeq[B]] {
  override def companion = MySeq  // Type Mismatch Here

  def iterator = s.iterator

  def apply(i: Int) = s(i)

  def length = s.length

  override def toString = s map { _.toString } mkString("\n")
}
object MySeq extends SeqFactory[MySeq] {
  implicit def canBuildFrom[B]: CanBuildFrom[Coll, B, MySeq[B]] =
       new GenericCanBuildFrom[B]
  // Type Mismatch in the line below
  def newBuilder[B] = new ListBuffer[B] mapResult (x => new MySeq(x.toSeq))
}


但是在指示的行中出现以下类型不匹配错误:

inferred type arguments [B] do not conform to
class MySeq's type parameter bounds [+B <: MyA]
Main.scala  line 49

type mismatch;
found   : countvotes.structures.MySeq.type
required: scala.collection.generic.GenericCompanion[Seq]
Main.scala  line 36

type mismatch;
found   : MySeq[B(in class MySeq)]
required: MySeq[B(in method newBuilder)]
Main.scala  line 49

type mismatch;
found   : scala.collection.immutable.Seq[B(in method newBuilder)]
required: Seq[B(in class MySeq)]
Main.scala  line 49


我试图通过将边界添加到CanBuildFrom和newBuilder的类型参数来解决此问题,但是随后出现其他错误消息。

如何创建绑定了类型参数的自定义Seq

最佳答案

我在第26行上没有收到错误:

override def companion = MySeq


也许是其他原因造成的。

无论如何,问题在于您不能拥有GenericCompanion[MySeq]SeqFactory的超类型)。原因是GenericCompanion[Coll]意味着您可以为任何Coll[A]构造A(请参见newBuilder的签名)。您也不能拥有MySeq[A] <: GenericTraversableTemplate[A, MySeq],因为genericBuilder是不可能的。这很有道理; MySeq并不是真正的“通用集合”,因为它希望其元素全部为MyA

解决方案是使用MySeq[B] <: GenericTraversableTemplate[B, Seq]extends Seq免费提供)。然后,companion有两个选择。它可以是Seq中的默认值,也可以是s.companion。在第一种情况下,((as: MySeq[A]): Seq[A]).map(...)将产生一个List(在运行时;在编译时只是一个通用的Seq)。在第二个中,它将取决于as.s是什么(再次在运行时;编译时将仅看到Seq)。不过,您可以保留extends SetLike

然后,您需要提供一个自定义的CanBuildFromMySeq.canBuildFrom[A <: MyA]: CanBuildFrom[MySeq[A], A, MySeq[A]],并定义MySeq#newBuilder

class MySeq[+B <: MyA](val s: Seq[B])
  extends Seq[B]
  with SeqLike[B, MySeq[B]]
{
  override def iterator = s.iterator
  override def apply(i: Int) = s(i)
  override def length = s.length

  override def toString = s.map(_.toString).mkString("\n")

  override def companion = s.companion
  protected[this] override def newBuilder: mutable.Builder[B, MySeq[B]] = new mutable.Builder[B, MySeq[B]] {
    private[this] val base = s.genericBuilder[B]
    override def +=(elem: B) = { base += elem; this }
    override def clear() = base.clear()
    override def result() = new MySeq[B](base.result())
  }
}

object MySeq {
  implicit def canBuildFrom[A <: MyA]: CanBuildFrom[MySeq[_], A, MySeq[A]] = ???
}

val list = List(new MyA, new MyA, new MyA, new MyA)
val vect = list.toVector
val mLst = new MySeq(list)
val mVec = new MySeq(vect)
{
  val res = mLst.filter(_.hashCode != list.head.hashCode)
  implicitly[res.type <:< MySeq[MyA]]
}
{
  val res = (mVec: Seq[MyA]).map(identity)
  assert(res.isInstanceOf[Vector[_]])
}
{
  val res = (mLst: Seq[MyA]).map(identity)
  assert(res.isInstanceOf[List[_]])
}

07-24 21:30