TheMP
TheMP

Reputation: 8427

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?

Upvotes: 6

Views: 2214

Answers (2)

Angelo Genovese
Angelo Genovese

Reputation: 3398

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) _
}

Upvotes: 6

adelbertc
adelbertc

Reputation: 7320

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

Upvotes: 5

Related Questions