Saya mencoba menggunakan jawaban pertanyaan sebelumnya untuk mengimplementasikan grafik kecil perpustakaan. Idenya adalah untuk menganggap grafik sebagai koleksi, di mana simpul membungkus elemen koleksi.
Saya ingin menggunakan tipe abstrak untuk mewakili tipe Vertex dan Edge (karena keamanan tipe) dan saya ingin menggunakan parameter tipe untuk mewakili tipe elemen koleksi (karena saya ingin mendefinisikannya saat instantiasi dengan mudah).
Namun, ketika mencoba contoh paling dasar yang dapat saya pikirkan, saya mengalami kesalahan kompilasi. Berikut ini contohnya:
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
}
}
Dan inilah implementasi dasarnya:
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 + ")"
}
}
Saat mencoba mengkompilasi, saya mendapatkan:
/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
Saya sama sekali tidak tahu arti dari kesalahan ini... Namun, jika saya mengkhususkan tipe T dalam implementasinya (class SimpleGraphKind extends GraphKind[Int]
saya hanya mendapatkan kesalahan pertama.
Apakah Anda punya ide?
vertex1.connectTo(vertex2)
atauvertex1 -> vertex2
daripadagraph.connect( vertex1, vertex2)
. Vertex dapat berisi logika atau hanya menjadi proksi ke objek grafik, bergantung pada implementasinya. - person paradigmatic   schedule 15.01.2010