Scala cake-pattern compilation error with Precog configuration template

Following this question, I have the following:

case class Pet(val name: String) trait ConfigComponent { type Config def config: Config } trait VetModule extends ConfigComponent { type Config <: VetModuleConfig def vet: Vet trait Vet { def vaccinate(pet: Pet) } trait VetModuleConfig { def extra: String } } trait VetModuleImpl extends VetModule { override def vet: Vet = VetImpl object VetImpl extends Vet { def vaccinate(pet: Pet) = println("Vaccinate:" + pet + " " + config.extra) } } 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 PetStoreModule extends ConfigComponent { type Config <: PetStoreConfig def petStore: PetStore trait PetStore { def sell(pet: Pet): Unit } trait PetStoreConfig { val petStoreName: String } } trait PetStoreModuleImpl extends PetStoreModule { self: VetModule with AnotherModule => override def petStore: PetStore = PetstoreImpl object PetstoreImpl extends PetStore { def sell(pet: Pet) { vet.vaccinate(pet) println(s"Sold $pet! [Store: ${config.petStoreName}, lastName: $getLastName]") } } } class MyApp extends PetStoreModuleImpl with VetModuleImpl with AnotherModuleImpl { type Config = PetStoreConfig with AnotherConfig override object config extends PetStoreConfig with AnotherConfig { val petStoreName = "MyPetStore" val lastName = "MyLastName" } petStore.sell(new Pet("Fido")) } object Main { def main(args: Array[String]) { new MyApp } } 

I get the following compilation error:

 value petStoreName is not a member of PetStoreModuleImpl.this.Config println(s"Sold $pet! [Store: ${config.petStoreName}, lastName: $getLastName]") ^ 

This is actually a mistake that I struggled with. Can someone explain why this is happening? Currently, as a workaround, I just explicitly used the configuration object in each module implementation.

+4
source share
2 answers

What you wrote should work, but not because of this error .

There are several workarounds you can use. Adding type Config = PetStoreConfig with AnotherConfig to your module implementation is probably a little less annoying than casting.

Update. As som-snytt notes in the comment and response, adding with PetStoreModuleImpl (not with PetStoreModule , as you might expect) to the end of the type itself is the best solution.

As a footnote: as discussed in the comments on SI-7255, Dependent Object Types calculus (which should be the “new” foundation for a system like Scala) will address this “fundamental problem in the Scala type system”.

+6
source

You can abort your type to save the member of the abstract type you want as the last win wins :

 trait PetStoreModuleImpl extends PetStoreModule { self: VetModule with AnotherModule with PetStoreModuleImpl => override def petStore: PetStore = PetstoreImpl object PetstoreImpl extends PetStore { def sell(pet: Pet) { vet.vaccinate(pet) println(s"Sold $pet! [Store: ${config.petStoreName}, lastName: $getLastName]") } } } 

He will then tell you that the vet module is not configured:

 class MyApp extends PetStoreModuleImpl with VetModuleImpl with AnotherModuleImpl { override type Config = PetStoreConfig with VetModuleConfig with AnotherConfig override object config extends PetStoreConfig with VetModuleConfig with AnotherConfig { val petStoreName = "MyPetStore" val lastName = "MyLastName" val extra = "vet-info" } petStore.sell(new Pet("Fido")) } 
+3
source

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


All Articles