In order to create a DSL for my new Scala project I've written the following code:
trait DocDB[O] {
def searchFor[I] (docs: Iterable[I], queryStrategy: QueryStrategy[I, DocDB[_]]): Iterable[(I,O)]
}
trait QueryStrategy[I, +F <: DocDB[_]]
class In // class of input documents
class Out // class of output documents
// MyDocDB
object MyDocDB extends DocDB[Out] {
def searchFor[I] (docs: Iterable[I], queryStrategy: QueryStrategy[I, DocDB[_]]) = List()
}
// MyQueryStrategy for In and MyDocDB
implicit object MyQueryStrategy extends QueryStrategy[In, MyDocDB.type]
implicit def listExt[I] (items: Iterable[I]) = new {
def findAt[O, F <: DocDB[O]](docDB: F) = new {
def apply(implicit queryStrategy: QueryStrategy[I, F]): Iterable[(I,O)] = {
docDB.searchFor[I](items, queryStrategy)
}
}
}
What I actually want is to be able to write
val out1: Iterable[(In, Out)] = List[In]() findAt MyDocDB
to find corresponding documents for my input documents at MyDocDB by using the query strategy MyQueryStrategy (defined as default for this input type and DocDB via implicit).
Unfortunately, the Scala compile has a problem with that line. It claims that it can not infer the types:
error: inferred type arguments [Nothing,test.StackOverflow.MyDocDB.type] do not conform to method findAt's type parameter bounds [O,F <: test.StackOverflow.DocDB[O]]
val out1: Iterable[(In, Out)] = List[In]() findAt MyDocDB
Somehow it infers Nothing
instead of Out
. How can I solve this without telling the compiler explicitly that it should assume O
to be of type Out
? I mean, the following works but does not lead to a concise DSL:
val out2: Iterable[(In, Out)] = List[In]().findAt[Out, MyDocDB.type](MyDocDB).apply(MyQueryStrategy)
Any suggestions?
Thanks very much for your answers. I finally took paradigmatic's solution, since it allows that query strategies can be targeted to specific DocDBs which I actually need in my project. In addition to paradigmatic's solution I replaced the listExt function by
implicit def listExt[I] (items: Iterable[I]) = new {
def findAt[F <: DocDB](docDB: F)(implicit queryStrategy: QueryStrategy[I, F]): Iterable[(I,F#O)] = {
docDB.searchFor[I](items, queryStrategy)
}
}
so that I can omit the apply method and implicit QueryStrategy:
val out1: Iterable [(In,Out)] = List[In]() findAt MyDocDB
Thanks again.
I have some success by turning O
in an abstract type in DocDB
and using type projection:
trait DocDB {
type O
def searchFor[I](
docs: Iterable[I],
queryStrategy: QueryStrategy[I, DocDB]
): Iterable[(I,O)]
}
trait QueryStrategy[I, +F <: DocDB]
class In // class of input documents
class Out // class of output documents
object MyDocDB extends DocDB {
type O = Out
def searchFor[I](
docs: Iterable[I],
queryStrategy: QueryStrategy[I, DocDB]
) = List()
}
implicit object MyQueryStrategy extends QueryStrategy[In, MyDocDB.type]
implicit def listExt[I] (items: Iterable[I]) = new {
def findAt[F <: DocDB](docDB: F) = new {
def apply(implicit queryStrategy: QueryStrategy[I, F]):
Iterable[(I,F#O)] = {
docDB.searchFor[I](items, queryStrategy)
}
}
}
It seems to work:
scala> val out1: Iterable [(In,Out)] = List[In]() findAt MyDocDB apply
out1: Iterable[(In, Out)] = List()