使用Precog配置模式的Scala cake-pattern编译错误

this问题开始,我现在有以下内容

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
  }
}

我得到以下编译错误

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

这实际上是我一直在努力的错误.有人可以解释它为什么会发生吗?目前,作为一种解决方法,我只是在每个模块实现中显式地转换配置对象.

解决方法

你写的东西应该有用,但不是,因为 this bug.

您可以使用许多变通方法.将Another Config = PetStoreConfig与AnotherConfig一起添加到模块实现中可能比铸造更不愉快.

更新:正如som-snytt在评论和回答中指出的那样,在自我类型的末尾添加PetStoreModuleImpl(关键不是PetStoreModule,如你所料),这是一个更好的解决方案.

作为脚注:正如在SI-7255的评论中所讨论的那样,Dependent Object Types calculus(被设计为“Scala类型系统的新基础”)将解决这个“Scala类型系统中的基本问题”.

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...