首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >具有路径依赖类型和协变参数的Scala复制类

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

Stack Overflow用户
提问于 2018-06-08 03:58:15
回答 1查看 66关注 0票数 0

我在路径依赖类型和协变参数方面遇到了一些问题。我需要创建一个新的SomeFancyCollection实例,但由于依赖于路径的类型,它无法编译。当我从类中取出像Node和Branch这样的东西时,我必须将root参数声明为privatethis,而且我也不能获得类的新实例。我的代码看起来像这样:

代码语言:javascript
复制
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
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 06:35:34

在您的代码中,没有明显的理由使NodeLeaf嵌套路径依赖类。只需使它们成为协变的独立类,那么路径依赖的所有问题就会消失:

代码语言:javascript
复制
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)
  }
}

如果不想污染名称空间,只需将Node类声明为package-private,或者甚至将所有这些辅助实现细节类隐藏在SomeFancyCollection的配套对象中

代码语言:javascript
复制
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] = ???
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50749309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档