創建一個參數化圖形類型,我想創建一個通用的類型層次的代表圖。特別是,我想喜歡有班圖和節點,我想,對於每一個圖表類型,有相應的節點類型,如果我創建操作圖在通用的功能,我想這個功能使用實際的節點類型。我試過在斯卡拉
trait GNode[Graph]
{
... functions to get edges from this vertex, etc. ...
}
trait Graph
{
type Node <: GNode[Graph]
}
def dfs[G <: Graph](g : G, nodeAction : G#Node => Unit) = ... code ...
但這並沒有工作,因爲當我做
class ConcreteGraph extends Graph
{
class Node extends GNode[ConcreteGraph] { ... }
}
DFS功能不會接受ConcreteGraph#Node=>Unit
類型的功能nodeAction
一個例子,但只有AnyRef=>Unit
或GNode[ConcreteGraph]=>Unit
。
更清晰,如果我在C++做的,我會做這樣的事情
template <class T> struct graph_traits;
template <> struct graph_traits<concrete_graph>
{ typedef concrete_graph::node node_type; }
template <class G>
void dfs(const G& g, boost::function<void(
const graph_traits<G>::node_type&)> action) { ... }
感謝。然而,我不確定爲什麼我需要在CGGrid中做類CGNode並在ConcreteGraph中輸入Node。我創建了一個小例子:http://snipt.org/vpk,似乎功能我 – jpalecek 2009-02-11 23:54:49