簡體   English   中英

具有路徑依賴類型和協變參數的Scala復制類

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

我在依賴路徑的類型和協變參數方面遇到了一些麻煩。 我需要創建一個新的SomeFancyCollection實例,但是由於依賴於路徑的類型而無法編譯。 當我從類中拉出諸如Node和Branch之類的所有東西時,我必須將根參數聲明為private [this],而Iam也無法獲取該類的新實例。 我的代碼如下:

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
  }
}

在您的代碼中,沒有明顯的理由使NodeLeaf嵌套與路徑相關的類。 只需使它們成為協變獨立類,然后所有與路徑相關的問題就會消失:

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的伴隨對象中:

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