pur*_*efn 8 scala typeclass dependent-method-type
我有一堆看起来完全相同的数据存储类型.
trait FooStore[C] {
def create(f: FooId => Foo)(c: C): Foo
// update and find methods
}
Run Code Online (Sandbox Code Playgroud)
我想简化一些事情,并希望使用依赖方法类型来获得更接近的东西
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
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试创建一个如下的实例时
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")
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下编译错误
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
Run Code Online (Sandbox Code Playgroud)
我不明白,因为该方法在DataStore实例上已经明确定义.错误是什么意思,这可能吗?如果没有,是否有不同的方法来完成同样的事情?