【发布时间】: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