Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - currying function with implicit value

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?

like image 934
TheMP Avatar asked Aug 12 '15 20:08

TheMP


2 Answers

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) _
}
like image 123
Angelo Genovese Avatar answered Oct 25 '22 13:10

Angelo Genovese


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

like image 33
adelbertc Avatar answered Oct 25 '22 12:10

adelbertc