Scala蛋糕模式编译错误与Precog配置模式 [英] Scala cake-pattern compile error with Precog config pattern

查看:185
本文介绍了Scala蛋糕模式编译错误与Precog配置模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题后,我现在有

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 is actually the error I have been struggling with. Can somebody explain why it occurs? Currently, as a work-around, I just explicitly cast the config object in each module implementation.

推荐答案

工作,但由于此错误 a>。

What you've written should work, but doesn't, because of this bug.

您可以使用多种解决方法。添加类型Config = PetStoreConfig with AnotherConfig 到您的模块实现可能是比投射不那么不愉快。

There are a number of workarounds you could use. Adding type Config = PetStoreConfig with AnotherConfig to your module implementation is probably a little less unpleasant than casting.

更新:作为注释和回答中的注释,添加与PetStoreModuleImpl (至关重要的不是与PetStoreModule ,正如你可能期望)到自我类型的结尾是一个更好的解决方案。

Update: As som-snytt notes in a comment and answer, adding with PetStoreModuleImpl (crucially not with PetStoreModule, as you might expect) to the end of the self-type is a better solution.

作为脚注:如SI-7255的注释中讨论的,依赖对象类型演算(设计为Scala的类型系统的新基础)将解决这个Scala类型的基本问题系统。

As a footnote: as discussed in the comments on SI-7255, the Dependent Object Types calculus (which is designed to be "a new foundation for Scala's type system") will address this "fundamental problem in Scala's type system".

这篇关于Scala蛋糕模式编译错误与Precog配置模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆