0

私は次のようなものを持っています:

trait Node[P <: Node[_]]

class RootNode extends Node[Null] {
   val refB : NodeB[RootNode] = ....
}

class NodeB[P <: Node[_]] extends Node[P] {
   val refC : NodeC[NodeB[P]] = ....
}

class NodeC[P <: Node[_]] extends Node[P] {
   val refD : NodeD[NodeC[P]] = ....
}

この種の構造に対処するためのより良い方法はありますか? どういうわけか、私のアプローチではP、直接の親レベルのみに制約するために使用できましたが、親の親を失ってしまった (など) ため、制約は可能な限りタイトではありません。すべてのコンテキストを失いたくない場合は、次のように変更する必要があります。

class NodeC[P <: Node[_]] extends Node[P] {
   val refD : NodeD[NodeC[NodeB[NodeA[RootNode]]] = ....
}

これは完全に実行不可能です。

私が試したアプローチはすべて、違法な循環参照につながりました。この問題の解決策はありますか?

4

1 に答える 1

1

制限を別の種類の構造として表現することを検討しましたか? その種のネストは確かに可能ですが、 を使用して実装できる懸念事項のように見えますHList

その方法で参照を表す代わりにNode、次のようなものを使用して単純に実装してみませんか。ここでは、非常に単純な形のないパターンで何ができるかについて、一般的な例をいくつか紹介します。

要件についてより具体的に説明できる場合は、ここにいる多くの人が支援するためにさらに多くのことを行うことができると確信していHListます.

import shapeless._
import shapeless.ops.hlist._
import shapeless.::

class Node[P <: Hlist](hl: P) {
   def append[T](obj: T): Node[P :: T] = new Node[P :: T](hl :: obj)

   // Much like a normal List, HList will prepend by default.
   // Meaning you need to reverse to get the input order.
   def reverse[Out]()(
     implicit rev: Reverse.Aux[P, Out]
   ): Out = rev(hl)

   // you can enforce type restrictions with equality evidence.
   // For instance you can use this to build a chain
   // and then make sure the input type matches the user input type.
   def equalsFancy[V1 <: Product, Rev, Out <: Product](v1: V1)(
     // We inverse the type of the HList to destructure it
     // and get the initial order.
     implicit rev: Reverse.Aux[P, Rev],
     // then convert it to a tuple for example.
     tp: Tupler.Aux[Rev, Out],
     ev: V1 =:= Out
   ): Boolean = tp(hl) == v1
}
object Node {
  def apply: Node[HNil] = new Node[HNil]

  Node().append[String]("test").append[Int](5).equalsFancy("test" -> 5)
}

Node同様にを使用して、リスト内の型要素を のサブタイプのみに制限するのは非常に簡単LUBConstraintです (型の下限)。

class NodeList[HL <: HList](list: Node[_] :: HL)(implicit val c: LUBConstraint[HL, Node[_])

_ <:< Node[_]これは、にない要素を追加NodeListできなくなったことを意味しますPoly

trait A
trait B
object printPoly extends Poly1 {
  // Let's assume these are your A, B and Cs
  // You can use Poly to define type specific behaviour.
  implicit def caseNodeA[N <: Node[A]] = at[N](node => println("This is an A node"))
  implicit def caseNodeB[N <: Node[B]] = at[N](node => println("This is a B node"))
implicit def unknown[N <: Node[_]] = at[N](node => println("This is not known to us yet"))
}
val nodeList: NodeList[..] = ..
nodeList.list.map(printPoly)

アップデート

その場合、ツリーのような構造を実装する価値があります。

  case class Node[A, F <: HList](value: A, children: F) {
    def addChild[T, FF <: HList](
      child: Node[T, FF]
    ): Node[A, HTree[T, FF] :: F] = {
      new Node(value, child :: children)
    }

    def values = Node.Values(this)
  }

  object Node {
    def apply[A](label: A) = new Node[A, HNil](label, HNil)

    object Values extends Poly1 {
      implicit def caseHTree[A, F <: HList, M <: HList](
        implicit fm: FlatMapper.Aux[getLabels.type, F, M],
          prepend: Prepend[A :: HNil, M]
        ): Case.Aux[HTree[A, F], prepend.Out] = 
          at[HTree[A, F]](tree => prepend(
            tree.value :: HNil,
            fm(tree.children))
          )
    }
  }
于 2016-07-27T19:21:53.133 に答える