Reputation: 488
For a DSL I need to implicitly extend function values. For instance:
trait PimpedFunction[-A, +B] extends Function1[A, B] {
def foo = 42
}
object PimpedFunction {
implicit def pimp[A, B](f: Function1[A, B]): PimpedFunction[A, B] =
new PimpedFunction[A, B] {
def apply(a: A) = f(a)
}
}
A function that uses PimpedFunction
could be defined as something like:
def takes(f: PimpedFunction[String, Int]) = f.foo
The problem is on the code that calls takes
. The following works as expected:
takes((_: String).size)
But if I elide the parameter type the compile fails to infer it:
takes(_.size)
Can I change anything in order to help scalac's inference?
OBS: The real use case is related to this: https://gist.github.com/xeno-by/4542402
Upvotes: 0
Views: 85
Reputation: 167891
If you can allow your takes
function to do the conversion itself, it'll work:
def takes(f: Function1[String, Int])(
implicit f2pf: Function1[String,Int] => PimpedFunction[String,Int]
) = f2pf(f).foo
(you can overload takes
to take either a PF or a regular function).
Upvotes: 3