2012-03-25 79 views
7

我有一堆看起来都一样的数据存储类型类。相关的方法类型和类型

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

+0

只是检查...你用'-Ydependent法-types'编译? – mergeconflict 2012-03-25 03:26:21

+0

@mergeconflict:是的,有依赖性的方法类型 – purefn 2012-03-25 03:43:54

回答

7

它编译使用的Scala-2.10-M2里程碑,一些从属方法类型错误已得到自2.9释放固定。我不完全确定,但也许this one可能使它的工作。

+0

我@Arjan同意编译......我的作品与最新的2.10.0-快照,SI-5033看起来有可能的罪魁祸首。 – 2012-03-25 08:31:56

+0

太棒了!多谢你们!那么2.9.2 RC呢?我没有一个可以轻松测试的环境。猜猜我必须尽快得到一个。 – purefn 2012-03-25 08:40:53