6

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?

2 Answers 2

6

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) _
}
Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

Yes, I know why it does not work, I am just curious if there is any way to walk around this problem and keep the code more or less clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.