Scala Existential Type Cake Template: Compilation Error

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?

0
source share
2 answers

Remove AnotherConfig definition from AnotherModuleImpl

0
source

You define AnotherConfig twice - once in AnotherModule and again in AnotherModuleImpl. These two definitions of a sign are different and are considered incompatible. The Config type is defined in terms of the first of them, but when you define MyApp, you set the type last - hence, an error.

You can fix this either by deleting the last AnotherConfig definition (as suggested by @rarry), or the last property extends the first (if you have a reason to save the latter, for example, define additional fields).

+1
source

Source: https://habr.com/ru/post/1497951/


All Articles