简体   繁体   English

具有路径依赖类型和协变参数的Scala复制类

[英]Scala copy class with path dependent types and covariant parameter

I have some trouble with path dependent types and covariant parameters. 我在依赖路径的类型和协变参数方面遇到了一些麻烦。 I need to create a new Instance of SomeFancyCollection but it won't compile because of the Path dependent types. 我需要创建一个新的SomeFancyCollection实例,但是由于依赖于路径的类型而无法编译。 When I pull all the stuff like Node and Branch out of the class i have to declare the root paramter as private[this] and Iam also not able to get a new Instance of the class. 当我从类中拉出诸如Node和Branch之类的所有东西时,我必须将根参数声明为private [this],而Iam也无法获取该类的新实例。 My code looks like: 我的代码如下:

class SomeFancyCollection[+A] {

  private var root: Node = Branch(0x0, Vector[Node]())

  trait Node {
    val id: Byte
    def put[A1 >: A](ids: Seq[Byte], item: A1): Node
    def remove[A1 >: A](ids: Seq[Byte], item: A1): Node
  }

  case class Branch(id: Byte, subs: Vector[Node]) extends Node {
      ...
  }

  case class Leaf[A1 >: A](id: Byte, subs: Vector[A1]) extends Node {
      ...
  }

  def add[A1 >: A](item: A1): SomeFancyCollection[A1] = {
    val ids: Seq[Byte] = getIds() // doesn't matter
    val newRoot = root.put(ids, item)
    val newInstance = new SomeFancyCollection[A1]()
    newInstance.root = newRoot
    newInstance
  }
}

In your code, there is no obvious reason to make Node and Leaf nested path-dependent classes. 在您的代码中,没有明显的理由使NodeLeaf嵌套与路径相关的类。 Just make them covariant stand-alone classes, then all the problems with path-dependence go away: 只需使它们成为协变独立类,然后所有与路径相关的问题就会消失:

trait Node[+A] {
  val id: Byte
  def put[A1 >: A](ids: Seq[Byte], item: A1): Node[A1]
  def remove[A1 >: A](ids: Seq[Byte], item: A1): Node[A1]
}

case class Branch[+A](id: Byte, subs: Vector[Node[A]]) extends Node[A] {

  def put[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
  def remove[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
}

case class Leaf[+A](id: Byte, subs: Vector[A]) extends Node[A] {

  def put[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
  def remove[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
}



class SomeFancyCollection[+A](val root: Node[A] = Branch(0x0, Vector[Node[A]]())) {
  def add[A1 >: A](item: A1): SomeFancyCollection[A1] = {
    val ids: Seq[Byte] = ???// getIds() // doesn't matter
    val newRoot = root.put(ids, item)
    new SomeFancyCollection[A1](newRoot)
  }
}

If you don't want to pollute the namespace, just declare the Node classes package-private, or even hide all those auxiliary implementation-detail classes inside the companion object of SomeFancyCollection : 如果您不想污染名称空间,只需声明Node类为package-private,甚至将所有这些辅助实现细节类都隐藏在SomeFancyCollection的伴随对象中:

class SomeFancyCollection[+A] private[SomeFancyCollection](
  val root: SomeFancyCollection.AnnoyingDetails.Node[A]
) {
  def add[A1 >: A](item: A1): SomeFancyCollection[A1] = {
    val ids: Seq[Byte] = ???// getIds() // doesn't matter
    val newRoot = root.put(ids, item)
    new SomeFancyCollection[A1](newRoot)
  }
}

object SomeFancyCollection {

  def empty[A]: SomeFancyCollection[A] = new SomeFancyCollection[A](
    AnnoyingDetails.Branch(0x0, Vector[AnnoyingDetails.Node[A]]())
  )

  private[SomeFancyCollection] object AnnoyingDetails {
    trait Node[+A] {
      val id: Byte
      def put[A1 >: A](ids: Seq[Byte], item: A1): Node[A1]
      def remove[A1 >: A](ids: Seq[Byte], item: A1): Node[A1]
    }

    case class Branch[+A](id: Byte, subs: Vector[Node[A]]) extends Node[A] {
      def put[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
      def remove[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
    }

    case class Leaf[+A](id: Byte, subs: Vector[A]) extends Node[A] {
      def put[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
      def remove[A1 >: A](ids: Seq[Byte], item: A1): Node[A1] = ???
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM