ฉันกำลังพยายามใช้คำตอบของคำถามก่อนหน้าเพื่อใช้กราฟขนาดเล็ก ห้องสมุด. แนวคิดก็คือการพิจารณากราฟเป็นคอลเลคชัน โดยที่จุดยอดจะล้อมองค์ประกอบคอลเลกชั่น
ฉันต้องการใช้ประเภทนามธรรมเพื่อแสดงประเภท Vertex และ Edge (เนื่องจากความปลอดภัยของประเภท) และฉันต้องการใช้พารามิเตอร์ประเภทเพื่อแสดงประเภทขององค์ประกอบการรวบรวม (เพราะฉันต้องการกำหนดพวกมันในการสร้างอินสแตนซ์อย่างง่ายดาย)
อย่างไรก็ตาม เมื่อลองตัวอย่างพื้นฐานที่สุดที่ฉันคิดได้ ฉันติดอยู่กับข้อผิดพลาดในการคอมไพล์ นี่คือตัวอย่าง:
package graph
abstract class GraphKind[T] {
type V <: Vertex[T]
type G <: Graph[T]
def newGraph(): G
abstract class Graph[T] extends Collection[T]{
self: G =>
def vertices(): List[V]
def add(t: T): Unit
def size(): Int
def elements(): Iterator[T]
}
trait Vertex[T] {
self: V =>
def graph(): G
def value(): T
}
}
และนี่คือการใช้งานพื้นฐาน:
class SimpleGraphKind[T] extends GraphKind[T] {
type G = GraphImpl[T]
type V = VertexImpl[T]
def newGraph() = new GraphImpl[T]
class GraphImpl[T] extends Graph[T] {
private var vertices_ = List[V]()
def vertices = vertices_
def add( t: T ) { vertices_ ::= new VertexImpl[T](t,this) }
def size() = vertices_.size
def elements() = vertices.map( _.value ).elements
}
class VertexImpl[T](val value: T, val graph: GraphImpl[T]) extends Vertex[T] {
override lazy val toString = "Vertex(" + value.toString + ")"
}
}
เมื่อพยายามคอมไพล์ ฉันจะได้รับ:
/prg/ScalaGraph/study/Graph.scala:10: error: illegal inheritance;
self-type GraphKind.this.G does not conform to Collection[T]'s selftype Collection[T]
abstract class Graph[T] extends Collection[T]{
^
/prg/ScalaGraph/study/Graph.scala:33: error: illegal inheritance;
self-type SimpleGraphKind.this.GraphImpl[T] does not conform to SimpleGraphKind.this.Graph[T]'s selftype SimpleGraphKind.this.G
class GraphImpl[T] extends Graph[T] {
^
/prg/ScalaGraph/study/Graph.scala:36: error: type mismatch;
found : SimpleGraphKind.this.VertexImpl[T]
required: SimpleGraphKind.this.V
def add( t: T ) { vertices_ ::= new VertexImpl[T](t,this) }
^
/prg/ScalaGraph/study/Graph.scala:38: error: type mismatch;
found : Iterator[T(in class SimpleGraphKind)]
required: Iterator[T(in class GraphImpl)]
def elements() = vertices.map( _.value ).elements
^
/prg/ScalaGraph/study/Graph.scala:41: error: illegal inheritance;
self-type SimpleGraphKind.this.VertexImpl[T] does not conform to SimpleGraphKind.this.Vertex[T]'s selftype SimpleGraphKind.this.V
class VertexImpl[T](val value: T, val graph: GraphImpl[T]) extends Vertex[T] {
^
5 errors found
ฉันไม่รู้ความหมายของข้อผิดพลาดเหล่านี้เลย... อย่างไรก็ตาม หากฉันเชี่ยวชาญประเภท T ในการใช้งาน (class SimpleGraphKind extends GraphKind[Int]
ฉันจะพบเฉพาะข้อผิดพลาดแรกเท่านั้น
คุณมีความคิดบ้างไหม?
vertex1.connectTo(vertex2)
หรือvertex1 -> vertex2
มากกว่าgraph.connect( vertex1, vertex2)
จุดยอดอาจมีตรรกะหรือเป็นเพียงพร็อกซีไปยังวัตถุกราฟ ขึ้นอยู่กับการใช้งาน - person paradigmatic   schedule 15.01.2010