我正在研究一个名为Graphlike
的图形特征,其中我对顶点使用依赖/关联类型。解决多态问题后,我的实现如下所示:
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] ^
遵循建议并制作 逆变给我带来了其他问题。这也不是我真正想要的;我希望我的构建器生产完全相同类型的自动机。
我在这里添加另一个答案,因为它与另一个答案明显不同,而且因为它会变得太大。这个使用类型类和隐式类来做所有事情,我觉得它更安全,即使它看起来很样板,对你来说有点太多了。
不同的对象不是subgraph
放在Graphlike
特征中,而是执行子图方法并构造新的自动机。隐式类提供了subgraph
方法,因为我无法证明G
是Graphlike
特征本身的this
类型。
斯卡斯蒂:https://scastie.scala-lang.org/bYTXEzS3T6uNMShDIjQghA
trait Graphlike {
type Vertex
}
trait Automaton extends Graphlike {
type State
type Vertex = State
def states: Iterable[State]
def initialState: State
}
class ConcreteAutomaton extends Automaton {
type State = Int
def states = List(1, 2, 3)
def initialState = 1
}
trait AutomatonBuilder[+A <: Automaton] {
def getAutomaton: A
}
class ConcreteAutomatonBuilder extends AutomatonBuilder[ConcreteAutomaton] {
def getAutomaton = new ConcreteAutomaton
}
trait Subgraph[G <: Graphlike] {
def subgraph(graph: G)(selectedVertices: Set[graph.Vertex]): G
}
trait AutomatonSubgraph[A <: Automaton] extends Subgraph[A] {
protected def newBuilder: AutomatonBuilder[A]
def subgraph(automaton: A)(selectedVertices: Set[automaton.Vertex]): A = {
val builder = newBuilder
//Do stuff to the builder here
builder.getAutomaton
}
}
implicit object ConcreteAutomatonSubgraph extends AutomatonSubgraph[ConcreteAutomaton] {
protected def newBuilder = new ConcreteAutomatonBuilder()
}
implicit class Subgraphable[+G <: Graphlike](graph: G)(implicit sub: Subgraph[G]) {
def subgraph(selectedVertices: Set[graph.Vertex]): G = sub.subgraph(graph)(selectedVertices)
}
class UsesAutomataAsGraphs {
val aut = new ConcreteAutomaton
val x = aut.subgraph(Set(aut.initialState)).subgraph(Set(aut.initialState))
}
另一种更忠实于您的原始设计但我不喜欢的方法:https://scastie.scala-lang.org/AfKX5cpbSXqrqesT4rsUvA
"构建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
}
Automaton
知道AutomatonBuilder
也很奇怪.
按照 Dmytro Mitin对另一个问题的回答,你可以通过让子图接受Set[A#Vertex]
而不是Set[this.Vertex]
来做到这一点。你不能像我在回答中建议的那样使用this.type
,因为你从中获得子图的原始自动机可能会实现你的构建者不可能知道的各种东西。
trait Graphlike[G <: Graphlike[G]] {
type Vertex
def subgraph(selectedVertices: Set[G#Vertex]): G
}
trait Automaton[A <: Automaton[A]] extends Graphlike[A] {
type State
type Vertex = State
def getBuilder: AutomatonBuilder[A]
def states: Iterable[State]
def initialState: State
def subgraph(selectedVertices: Set[A#Vertex]): A = {
val builder = getBuilder
// Some logic to actually do something to the builder here
builder.addVertices(selectedVertices) //Example
builder.getAutomaton
}
}
trait AutomatonBuilder[A <: Automaton[A]] {
def getAutomaton: A
//example
val verts = Set[A#Vertex]()
def addVertices(s: Set[A#Vertex]): Unit = verts ++ s
}
class ConcreteAutomaton extends Automaton[ConcreteAutomaton] {
type State = Int
def states = List(1, 2, 3)
def initialState = 1
val getBuilder = new ConcreteAutomatonBuilder
}
class ConcreteAutomatonBuilder extends AutomatonBuilder[ConcreteAutomaton] {
def getAutomaton = new ConcreteAutomaton
}
class UsesAutomataAsGraphs {
val aut: ConcreteAutomaton = new ConcreteAutomaton
aut.subgraph(Set(aut.initialState)).subgraph(Set(aut.initialState))
}