Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent method types and type-classes

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?

like image 356
purefn Avatar asked Mar 25 '12 03:03

purefn


People also ask

Does Scala have dependent types?

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.

What is dependent function?

A dependent function type is a function type whose result depends on the function's parameters.

What is dependent function in Python?

A dependent function type describes function types, where the result type may depend on the function's parameter values.


1 Answers

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.

like image 127
Arjan Blokzijl Avatar answered Oct 15 '22 07:10

Arjan Blokzijl