par*_*tic 9 generics types scala abstract-type
我试图使用前面问题的答案来实现一个小图库.我们的想法是将图形视为一种选择,其中顶点包裹集合元素.
我想使用抽象类型来表示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
}
}
Run Code Online (Sandbox Code Playgroud)
这是基本的实现:
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 + ")"
}
}
Run Code Online (Sandbox Code Playgroud)
在尝试编译时,我得到:
/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
Run Code Online (Sandbox Code Playgroud)
我完全不知道这些错误的含义......但是,如果我在实现中专门化类型T(class SimpleGraphKind extends GraphKind[Int]我只得到第一个错误.
你有什么想法吗?
Dan*_*ral 11
用以下结果编译-explaintypes:
<console>:11: 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]{
^
GraphKind.this.G <: Collection[T]?
Iterable[T] <: Iterable[T]?
T <: T?
T <: Nothing?
<notype> <: Nothing?
false
Any <: Nothing?
<notype> <: Nothing?
false
false
false
Any <: T?
Any <: Nothing?
<notype> <: Nothing?
false
false
false
false
false
GraphKind.this.Graph[T] <: Iterable[T]?
Iterable[T] <: Iterable[T]?
T <: T?
T <: Nothing?
<notype> <: Nothing?
false
Any <: Nothing?
<notype> <: Nothing?
false
false
false
Any <: T?
Any <: Nothing?
<notype> <: Nothing?
false
false
false
false
false
false
false
Run Code Online (Sandbox Code Playgroud)
现在,我即将写作我不明白怎么T <: T可能是假的 - 它几乎就像T被定义了两次,当然,这是整个问题.这里:
abstract class GraphKind[T] {
type V <: Vertex[T]
type G <: Graph[T]
def newGraph(): G
abstract class Graph[T] extends Collection[T]{
Run Code Online (Sandbox Code Playgroud)
好的,类GraphKind是参数化的T,类型G必须是a Graph[T].现在,class Graph也被参数化,并且它的参数也被调用T.为了防止混淆,让我们重写它:
abstract class Graph[T2] extends Collection[T2]{
self: G =>
def vertices(): List[V]
def add(t: T2): Unit
def size(): Int
def elements(): Iterator[T2]
}
Run Code Online (Sandbox Code Playgroud)
请注意,这与您所写的完全相同.我只是为类型参数使用了一个不同的名称,因此它不会与T参数化相混淆GraphKind.
所以,这是逻辑:
G <: Graph[T]
Graph[T2] <: Collection[T2]
Graph[T2] <: G // self type
Run Code Online (Sandbox Code Playgroud)
这意味着
Graph[T2] <: Graph[T]
Run Code Online (Sandbox Code Playgroud)
而且,因为Graph扩展Collection:
Collection[T2] <: Collection[T]
Run Code Online (Sandbox Code Playgroud)
但不能保证这是真的.我不明白为什么在没有继承时问题不会出现.固定:
abstract class GraphKind[T] {
type V <: Vertex
type G <: Graph
def newGraph(): G
abstract class Graph extends Collection[T]{
self: G =>
def vertices(): List[V]
def add(t: T): Unit
def size(): Int
def elements(): Iterator[T]
}
trait Vertex {
self: V =>
def graph(): G
def value(): T
}
}
class SimpleGraphKind[T] extends GraphKind[T] {
type G = GraphImpl
type V = VertexImpl
def newGraph() = new GraphImpl
class GraphImpl extends Graph {
private var vertices_ = List[V]()
def vertices = vertices_
def add( t: T ) { vertices_ ::= new VertexImpl(t,this) }
override def size() = vertices_.size
override def elements() = vertices.map( _.value ).elements
}
class VertexImpl(val value: T, val graph: GraphImpl) extends Vertex {
override lazy val toString = "Vertex(" + value.toString + ")"
}
}
Run Code Online (Sandbox Code Playgroud)
由于Vertex并将Graph绑定到一个实例GraphKind,然后T将固定为为该实例定义的任何内容.例如:
scala> new SimpleGraphKind[Int]
res0: SimpleGraphKind[Int] = SimpleGraphKind@1dd0fe7
scala> new res0.GraphImpl
res1: res0.GraphImpl = line10()
scala> res1.add(10)
scala> res1.add("abc")
<console>:9: error: type mismatch;
found : java.lang.String("abc")
required: Int
res1.add("abc")
^
Run Code Online (Sandbox Code Playgroud)