I am trying to write a query library for Scala. Here is the code so far:
class Query[TElement](source: Traversable[TElement]) {
def join[TOther](other: Traversable[TOther]) = new {
def on[TKey](keySelector1: TElement => TKey) = new {
def equals(keySelector2: TOther => TKey) = new {
def into[TResult](resultSelector: (TElement, TOther) => TResult): Query[TResult] = {
val map = source.map(e => (keySelector1(e), e)).toMap
val results = other
.map(e => (keySelector2(e), e))
.filter(p => map.contains(p._1))
.map(p => (map(p._1), p._2))
.map(p => resultSelector(p._1, p._2))
new Query[TResult](results)
}
}
}
}
}
object Query {
def from[TElement](source: Traversable[TElement]): Query[TElement] = {
new Query[TElement](source)
}
}
...
val results = Query.from(users)
.join(accounts).on(_.userId).equals(_.ownerUserId).into((_, _))
I get the following error when I go to compile:
error: missing parameter type for expanded function ((x$2) => x$2.ownerUserId)
I am a little confused why I would get this error on the non-generic function equals. Its generic parameters come from the outer scope, I'd think. I know to fix it I have to explicitly say what the parameter type is by writing (a: Account) => a.ownerUserId. However, I am trying to make it a pretty fluent library, and this is making it messy.