【发布时间】:2019-04-19 13:14:41
【问题描述】:
我想丰富一个“scala 图表”图表。为此,我创建了一个隐式值类:
import scalax.collection.mutable
import scalax.collection.edge.DiEdge
...
type Graph = mutable.Graph[Int, DiEdge]
implicit class EnrichGraph(val G: Graph) extends AnyVal {
def roots = G.nodes.filter(!_.hasPredecessors)
...
}
...
问题在于其方法的返回类型,例如:
import ....EnrichGraph
val H: Graph = mutable.Graph[Int,DiEdge]()
val roots1 = H.nodes.filter(!_.hasPredecessors) // type Iterable[H.NodeT]
val roots2 = H.roots // type Iterable[RichGraph#G.NodeT] !!
val subgraph1 = H.filter(H.having(roots1)) // works!
val subgraph2 = H.filter(H.having(roots2)) // type mismatch!
原因是否在于“Graph”具有依赖的子类型,例如节点T?有没有办法让这种充实发挥作用?
【问题讨论】: