generics - Scala error: missing parameter type for expanded function -
i trying write query library scala. here code 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 following error when go compile:
error: missing parameter type expanded function ((x$2) => x$2.owneruserid)
i little confused why error on non-generic function equals
. generic parameters come outer scope, i'd think. know fix have explicitly parameter type writing (a: account) => a.owneruserid
. however, trying make pretty fluent library, , making messy.
the problem quite simple. there ambiguity existing method equals
inherited any
. simple example:
scala> class x[a, b] { def equals(f: => b) = f } defined class x scala> val x = new x[int, string] x: x[int,string] = x@52d455b8 scala> x.equals((x: int) => x.tostring) res0: int => string = <function1> scala> x.equals((x: string) => x.tostring) // uh-oh res1: boolean = false
as 1 can see in last example, when wrong function type passed, compiler has choose def equals(any): boolean
. when don't specify type, compiler has infer one, can't in example.
simply rename method else , problem gone.
Comments
Post a Comment