通过this问题,我找到了this Precog 关于“配置”模式的文章。我用两个模块尝试了这个:
case class Pet(val name: String)
trait ConfigComponent {
type Config
def config: Config
}
trait Vet {
def vaccinate(pet: Pet) = {
println("Vaccinate:" + pet)
}
}
trait AnotherModule extends ConfigComponent {
type Config <: AnotherConfig
def getLastName(): String
trait AnotherConfig {
val lastName: String
}
}
trait AnotherModuleImpl extends AnotherModule {
override def getLastName(): String = config.lastName
trait AnotherConfig {
val lastName: String
}
}
trait PetStoreModule extends ConfigComponent {
type Config <: PetStoreConfig
def sell(pet: Pet): Unit
trait PetStoreConfig {
val vet: Vet
val name: String
}
}
trait PetStoreModuleImpl extends PetStoreModule {
override def sell(pet: Pet) {
println(config.name)
config.vet.vaccinate(pet)
// do some other stuff
}
}
class MyApp extends PetStoreModuleImpl with AnotherModuleImpl {
type Config = PetStoreConfig with AnotherConfig
override object config extends PetStoreConfig with AnotherConfig {
val vet = new Vet {}
val name = "MyPetStore"
val lastName = "MyLastName"
}
sell(new Pet("Fido"))
}
object Main {
def main(args: Array[String]) {
new MyApp
}
}
但是,我得到了这个编译错误:
overriding type Config in trait AnotherModule with bounds <: MyApp.this.AnotherConfig;
type Config has incompatible type
type Config = PetStoreConfig with AnotherConfig
我不清楚为什么这不起作用(Precog 在他们的示例中也使用了两个组件),有什么想法吗?
最佳答案
您定义了 AnotherConfig 两次——一次在 AnotherModule 中,一次在 AnotherModuleImpl 中。这两个特征的定义是不同的,被认为是不相容的。 Config 类型是根据前者定义的,但是当您定义 MyApp 时,您将类型设置为后者 - 因此会出现错误。
您可以通过删除 AnotherConfig 的后者定义(如@rarry 所建议)或让后者特征扩展前者(如果您有某些理由保留后者,例如定义额外字段)来修复它。
关于存在类型 : compile error 的 Scala 蛋糕模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330042/