Through this question, I found this article about the 'config' template from Precog. I tried this with two modules:
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 } }
However, I get this compilation code:
overriding Config type in trait AnotherModule with restrictions <: MyApp.this.AnotherConfig;
type Config has an incompatible type
type Config = PetStoreConfig using AnotherConfig
I donβt understand why this should not work (Precog also uses two components in its example), any ideas?
source share