I've approached a problem with scala implicits and I am posting a simplified version here.
I have a class called SomeClass that contains a implicit val a = 3;
I have a created a trait that I intend to use with SomeClass like below:
trait TestSum {
def sum(a:Int)(b:Int)(implicit c: Int): Unit = {
a + b + c
}
val sum_a = sum(1) _
}
Sum_a obviously will not work, because it requires an implicit value c, which is accessible in SomeClass scope. Moving sum_a to SomeClass solves the issue but messes a bit in my code. Is there any good solution to this?
You could require the implicit to exist in the implementor like this:
trait TestSum {
implicit def c: Int
def sum(a:Int)(b:Int): Unit = {
a + b + c
}
val sum_a = sum(1) _
}
Functions in Scala cannot have implicit parameters, so when you do eta-expansion (the _
) on a method to turn it into a function, it will look right then and there for a valid implicit parameter in scope to fill it in. As it stands, your code does not have an implicit Int
in scope so the expansion fails. If you move it into SomeClass
which does have an implicit Int
, it will use that Int
where the implicit is expected.
For instance:
scala> def foo(i: Int)(implicit s: String): String = i.toString ++ " " ++ s
foo: (i: Int)(implicit s: String)String
scala> foo _
<console>:10: error: could not find implicit value for parameter s: String
foo _
^
scala> implicit val s: String = "number"
s: String = number
scala> foo _
res1: Int => String = <function1>
scala> res1(5)
res2: String = 5 number
More info can be found here: http://tpolecat.github.io/2014/06/09/methods-functions.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With