加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 依赖方法类型和类型类

发布时间:2020-12-16 08:57:48 所属栏目:安全 来源:网络整理
导读:我有一堆看起来完全相同的数据存储类型. trait FooStore[C] { def create(f: FooId = Foo)(c: C): Foo // update and find methods} 我想简化一些事情,并希望使用依赖方法类型来获得更接近的东西 sealed trait AR { type Id type Type}sealed trait FooAR ex
我有一堆看起来完全相同的数据存储类型.

trait FooStore[C] {
  def create(f: FooId => Foo)(c: C): Foo
  // update and find methods
}

我想简化一些事情,并希望使用依赖方法类型来获得更接近的东西

sealed trait AR {
  type Id
  type Type
}

sealed trait FooAR extends AR {
  type Id = FooId
  type Type = Foo
}

trait DataStore[C] {
  def create(ar: AR)(f: ar.Id => ar.Type)(c: C): ar.Type
}

但是当我尝试创建一个如下的实例时

case class InMemory(foos: List[Foo])
object InMemory {
  lazy val InMemoryDataStore: DataStore[InMemory] = new DataStore[InMemory] {
    def create(ar: AR)(f: ar.Id => ar.Type)(c: InMemory): ar.Type = sys.error("not implemented")
  }
}

我得到以下编译错误

object creation impossible,since method create in trait DataStore of type (ar: AR)(f: ar.Id => ar.Type)(c: InMemory)ar.Type is not defined
  lazy val InMemoryDataStore: DataStore[InMemory] = new DataStore[InMemory] {
                                                        ^
one error found

我不明白,因为在DataStore实例上非常明确地定义了该方法.错误是什么意思,这可能吗?如果没有,是否有不同的方法来完成同样的事情?

解决方法

它使用Scala-2.10-M2里程碑进行编译,自2.9版本以来,一些依赖的方法类型错误已得到修复.我不完全确定,但也许 this one可能已经成功了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读