Scala蛋糕模式与Existential类型:编译错误 [英] Scala cake pattern with Existential Types: compile error

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

问题描述

透过这个问题,我发现这篇关于Precog的'config'模式的文章。我尝试这两个模块:

Through this question, I found this article on the 'config' pattern from Precog. I tried this with two modules:

case class Pet(val name: String)

trait ConfigComponent {
  type Config

  def config: Config
}

trait Vet {
  def vaccinate(pet: Pet) = {
    println("Vaccinate:" + pet)
  }
}


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 AnotherConfig {
    val lastName: String
  }

}

trait PetStoreModule extends ConfigComponent {
  type Config <: PetStoreConfig

  def sell(pet: Pet): Unit

  trait PetStoreConfig {
    val vet: Vet
    val name: String
  }

}

trait PetStoreModuleImpl extends PetStoreModule {
  override def sell(pet: Pet) {
    println(config.name)
    config.vet.vaccinate(pet)
    // do some other stuff
  }
}

class MyApp extends PetStoreModuleImpl with AnotherModuleImpl {

  type Config = PetStoreConfig with AnotherConfig

  override object config extends PetStoreConfig with AnotherConfig {
    val vet = new Vet {}
    val name = "MyPetStore"
    val lastName = "MyLastName"
  }

  sell(new Pet("Fido"))
}


object Main {
  def main(args: Array[String]) {
    new MyApp
  }
}

但是,我得到这个编译错误: / p>

However, I get this compile errror:


覆盖类型Config in trait具有边界的AnotherModule< ;: MyApp.this.AnotherConfig;

类型Config具有不兼容类型

类型Config = PetStoreConfig with AnotherConfig

overriding type Config in trait AnotherModule with bounds <: MyApp.this.AnotherConfig;
type Config has incompatible type
type Config = PetStoreConfig with AnotherConfig

我不清楚为什么这不应该工作

It is not clear to me why this should not work (Precog also uses two components in their example), any ideas?

推荐答案

从AnotherModuleImpl中删除AnotherConfig的定义

Remove definition of AnotherConfig from AnotherModuleImpl

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

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