简体   繁体   English

依赖方法类型和类型类

[英]Dependent method types and type-classes

I've got a bunch of data store type-classes that look all the same. 我有一堆看起来完全相同的数据存储类型。

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

I'd like to simplify things and was hoping to use dependent method types to get something closer to 我想简化一些事情,并希望使用依赖方法类型来获得更接近的东西

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
}

but when I try and create an instance of that as follows 但是当我尝试创建一个如下的实例时

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")
  }
}

I get the following compile error 我得到以下编译错误

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

I don't understand since that method is pretty clearly defined on the DataStore instance. 我不明白,因为在DataStore实例上非常明确地定义了该方法。 What does the error mean and is this possible? 错误是什么意思,这可能吗? If not, is there a different way to accomplish the same thing? 如果没有,是否有不同的方法来完成同样的事情?

It compiles using the Scala-2.10-M2 milestone, some dependent method type bugs have been fixed since the 2.9 release. 它使用Scala-2.10-M2里程碑进行编译,自2.9版本以来,一些依赖的方法类型错误已得到修复。 I'm not completely sure, but perhaps this one might have made it work. 我不完全确定,但也许这个可能会让它发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM