Let’s say that I want to create a simple graph representation where Vertex and Edge and their types depend on each other. A concrete implementation could look something like this:
case class Edge(id: Int, label: String, endpoints: (Vertex, Vertex))
case class Vertex(id: Int, data: Data, edges: Map[Int, Edge])
Edge depends on Vertex and vice versa. What I really want is for id, data etc to have generic types. And I wonder how to design this the best way?
trait Vertex[A, B] {
def id: A
def data: B
// What about types for the edges etc?
}
trait Edge[A, ...] {
def id: A
def label: String
def endpoints: (Vertex[...], Vertex[...])
}
A simple example of this would be much appreciated.
Since Vertex and Edge can have different ID types, you may want to have Vertex and Edge be inner traits of a Graph trait, which has all the type parameters. That way, Vertex can know about the Edge trait’s ID type, and Edge can know about the Vertex trait’s ID and Data types.
Incidentally, due to path-dependent types, Edge would only be able to have endpoints which are in the same Graph, and Vertex would only be able to have edges which are in the same Graph.