Scala cake-pattern compile error with Precog config pattern

前端 未结 2 2009
[愿得一人]
[愿得一人] 2021-01-19 16:37

Following from this question, I have now the following:

case class Pet(val name: String)

trait ConfigComponent {
          


        
2条回答
  •  后悔当初
    2021-01-19 16:55

    You can wrangle your self-type to keep the abstract type member you want, since the last bound 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]")
        }
      }
    }
    

    Then it will tell you that the vet module isn't 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"))
    }
    

提交回复
热议问题