背景信息:我目前正试图建立一个通用图库,其中包括几种不同的搜索算法(我已经开始使用Dijkstra的)。我已经建立了几个特征来表示会在某些类型的图表中找到方法(如权重,指导):斯卡拉未能推断出正确的类型参数
trait GraphOps[V,E] { ... }
trait WeightedGraphOps[V,E] extends GraphOps[V,E] { ... }
trait DirectedGraphOps[V,E] extends GraphOps[V,E] { ... }
object GraphOps{
def Dijkstra[V,E,G <: WeightedGraphOps[V,E] with DirectedGraphOps[V,E]](graph:G, start:V) = { ... }
}
在其他地方,我有一个类的具体实现加权,有向图的我想上运行Dijkstra算法:
class GraphMap[T](...)
extends scala.collection.mutable.Map[Position,T]
with WeightedGraphOps[Position,Edge] with DirectedGraphOps[Position,Edge] { ... }
但是,当我试图测试一下:
val graph = new GraphMap[Int](...)
val (dist, prev) = GraphOps.Dijkstra(graph, Position(0,0))
问题:在编译期间我收到以下错误:error: inferred type arguments [com.dylan.data.Position,Nothing,com.dylan.data.GraphMap[Int]] do not conform to method Dijkstra's type parameter bounds [V,E,G <: com.dylan.data.WeightedGraphOps[V,E] with com.dylan.data.DirectedGraphOps[V,E]]
我花了很长时间才发现它推断我的Edge(E
)类型为Nothing
,但我不明白为什么它无法成功推断它应该是Edge
。为什么它无法推断该类型参数,我该如何解决它?
P.S.我试着做以下,并得到它的工作,但是这似乎可怕的不方便什么应该是一个方便的方法:
type Helpful = WeightedGraphOps[Position,Edge] with DirectedGraphOps[Position,Edge]
val (dist, prev) = GraphOps.Dijkstra[Position,Edge,Helpful](graph, Position(0,0))
你知道这里有一个图库,不是吗? –
因为我这样做是为了爱好,而且因为几分钟的四处搜索并没有导致明确的答案,所以我想我可能会把自己放在一起。 – Dylan
我很好奇你什么条件,你完全谷歌?我试过'scala图库',并得到了这个:http://code.google.com/p/scala-graphs/对你来说不是这样吗? – AndreasScheinert