相关的方法类型和类型
问题描述:
我有一堆看起来都一样的数据存储类型类。相关的方法类型和类型
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法-types'编译? – mergeconflict 2012-03-25 03:26:21
@mergeconflict:是的,有依赖性的方法类型 – purefn 2012-03-25 03:43:54