Scala cake-pattern 编译错误与 Precog 配置模式

标签 scala compiler-errors existential-type 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 .

您可以使用多种解决方法。添加 type Config = PetStoreConfig with AnotherConfig到你的模块实现可能比强制转换要少一些不愉快。

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

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

关于Scala cake-pattern 编译错误与 Precog 配置模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330848/

相关文章:

haskell - 在运行时使用存在类进行杂耍

oop - Scala扩展参数化抽象类

scala - 尝试在 Isabelle/HOL 的 scala-isabelle 中加载 Imperative_Quicksort 理论时出现“未定义常量 : "eq" simpdata. ML”

scala - Akka Http : Exceeded configured max-open-requests value of [32]

Scala错误: missing parameter type for expanded function

c++ - 错误: "couldn' t infer template argument '_Tp' "when passing in {} into

arrays - 为什么 Scalaz 中没有 Array 的 Functor 实例

c++ - 递归 boost::variant 类型不能用 "-std=c++11 -stdlib=libc++"编译

haskell - attoparsec 如何返回不同类型的值?

scala - 存在类型如何与路径依赖类型重叠?