2013-08-20 48 views
0

通过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

回答

0

删除定义您可以定义AnotherConfig两次。这两个特征的定义是不同的,并且被认为是不相容的。 Config类型是根据前者来定义的,但是当你定义MyApp时,你需要将类型设置为后者 - 因此是错误。

您可以通过删除AnotherConfig的后一个定义(如@rarry所示)或使后者的特征扩展前者(如果您有某些原因保留后者,如定义额外字段)来修复它。

+0

谢谢你的回答,但是,我试图重现以下编译错误:值petStoreName不是PetStoreModuleImpl.this.Config的成员。请参阅[这里](https://gist.github.com/damirv/6278827)了解产生此编译错误的代码。任何想法为什么? – damirv

+0

我已经发布了这个[这里]的后续行动(http://stackoverflow.com/questions/18330848/scala-cake-pattern-compile-error-with-precog-config-pattern) – damirv

1

AnotherConfig的

+0

感谢您的解释,我发布了这个[这里]的后续(http://stackoverflow.com/questions/18330848/scala-cake-pattern-compile-error-with-precog-config-pattern) – damirv