通过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
}
}
不过,我得到这个编译errror:
overriding type Config in trait AnotherModule with bounds <: MyApp.this.AnotherConfig;
type Config has incompatible type
type Config = PetStoreConfig with AnotherConfig
这是我不明白为什么这不应该工作(Precog也使用两个部件在他们的例子) , 有任何想法吗?在AnotherModuleImpl一次AnotherModule,再 - 从AnotherModuleImpl
谢谢你的回答,但是,我试图重现以下编译错误:值petStoreName不是PetStoreModuleImpl.this.Config的成员。请参阅[这里](https://gist.github.com/damirv/6278827)了解产生此编译错误的代码。任何想法为什么? – damirv
我已经发布了这个[这里]的后续行动(http://stackoverflow.com/questions/18330848/scala-cake-pattern-compile-error-with-precog-config-pattern) – damirv