8
votes

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. What does the error mean and is this possible? If not, is there a different way to accomplish the same thing?

1
just checking... are you compiling with -Ydependent-method-types?mergeconflict
@mergeconflict: yes, compiling with dependent method typespurefn

1 Answers

7
votes

It compiles using the Scala-2.10-M2 milestone, some dependent method type bugs have been fixed since the 2.9 release. I'm not completely sure, but perhaps this one might have made it work.