我正在研究一个称为Graphlike
的图特征,其中我对顶点使用从属/关联类型。在solving my polymorphism issues之后,我的实现如下所示:
trait Graphlike {
type Vertex
def subgraph(selectedVertices: Set[Vertex]): this.type
}
我也有各种抽象的原子态自动机,它们的行为当然类似于图形:
trait Automaton extends Graphlike {
type State
type Vertex = State
def states: Iterable[State]
def initialState: State
def getBuilder: AutomatonBuilder[this.type]
def subgraph(selectedVertices: Set[Vertex]) = {
val builder = getBuilder
// Some logic to actually do something to the builder here
builder.getAutomaton
}
}
trait AutomatonBuilder[A <: Automaton] {
def getAutomaton: A
}
但是,当我尝试实际实现一个具体的自动机时,我遇到了麻烦:
class ConcreteAutomaton extends Automaton {
type State = Int
def states = List(1, 2, 3)
def initialState = 1
def getBuilder = new ConcreteAutomatonBuilder
}
class ConcreteAutomatonBuilder extends AutomatonBuilder[ConcreteAutomaton] {
def getAutomaton = new ConcreteAutomaton
}
class UsesAutomataAsGraphs {
val aut = new ConcreteAutomaton
aut.subgraph(Set(aut.initialState)).subgraph(Set(aut.initialState))
}
给予:
[info] Compiling 1 Scala source to /Users/albin/Downloads/example/target/scala-2.12/classes ...
[error] /Users/albin/Downloads/example/src/main/scala/example/Example.scala:33:20: type mismatch;
[error] found : ConcreteAutomatonBuilder
[error] required: AutomatonBuilder[ConcreteAutomaton.this.type]
[error] Note: ConcreteAutomaton >: ConcreteAutomaton.this.type (and ConcreteAutomatonBuilder <: AutomatonBuilder[ConcreteAutomaton]), but trait AutomatonBuilder is invariant in type A.
[error] You may wish to define A as -A instead. (SLS 4.5)
[error] def getBuilder = new ConcreteAutomatonBuilder
[error] ^
遵循建议并设定A的对立会给我带来其他问题。这也不是我真正想要的。我想要我的建造者生产完全相同类型的自动机。
“ this.type
的构建实例”听起来很奇怪。无论如何:
trait Graphlike {
type Vertex
def subgraph(selectedVertices: Set[Vertex]): this.type
}
trait Automaton extends Graphlike {
type State
type Vertex = State
def states: Iterable[State]
def initialState: State
def getBuilder: AutomatonBuilder[this.type]
def subgraph(selectedVertices: Set[Vertex]): this.type = {
val builder = getBuilder
// Some logic to actually do something to the builder here
builder.getAutomaton
}
}
trait AutomatonBuilder[A <: Automaton] {
def getAutomaton: A
}
class ConcreteAutomaton extends Automaton {
type State = Int
def states = List(1, 2, 3)
def initialState = 1
def getBuilder = new ConcreteAutomatonBuilder[this.type](this)
}
class ConcreteAutomatonBuilder[A <: Automaton with Singleton](a: A) extends AutomatonBuilder[A] {
def getAutomaton = a
}