[英]Higher kinded type chained, possible?
我有類似的東西:
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]]] = ....
}
這是完全不可行的。
我嘗試過的任何方法都導致我使用了非法的循環引用。 有什么解決辦法嗎?
您是否考慮過將限制表示為另一種結構? 確實可以進行這種嵌套,但是看起來您可以使用HList
實現這種關注。
與其以這種方式表示引用,不如通過使用類似以下內容的方法簡單地實現Node
的實現。 我在這里提供一些通用示例,說明可以使用超簡單的無形模式完成的工作。
如果您可以更具體地了解需求,那么我敢肯定,這里的許多人可以提供更多幫助,我的直覺告訴我HList
周圍有一種更簡單的方法,可以解決您的問題而無需進行任何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)
}
也可以通過使用LUBConstraint
將列表中的type元素限制為僅Node
的子類型(類型的下界)。
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))
)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.