【问题标题】:Dependent method types and type-classes依赖方法类型和类型类
【发布时间】:2012-03-25 03:19:33
【问题描述】:

我有一堆看起来都一样的数据存储类型类。

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 实例上定义得很清楚。错误是什么意思,这可能吗?如果没有,是否有不同的方法来完成同样的事情?

【问题讨论】:

  • 只是检查...你是用-Ydependent-method-types编译吗?
  • @mergeconflict:是的,使用依赖的方法类型进行编译

标签: scala dependent-method-type typeclass


【解决方案1】:

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

【讨论】:

  • 我同意 @Arjan ... 它适用于我最新的 2.10.0-SNAPSHOT,SI-5033 看起来确实是罪魁祸首。
  • 太棒了!多谢你们! 2.9.2 RC 怎么样?我没有一个可以轻松测试的环境。我想我很快就会得到一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多