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?
Scala has a notion of a type dependent on a value. This dependency is not expressed in the type signature but rather in the type placement.
A dependent function type is a function type whose result depends on the function's parameters.
A dependent function type describes function types, where the result type may depend on the function's parameter values.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With