Scala Functional Literals with Implicits

scala>  val sum2 = (a: Int) => {implicit b: Int => a + b}
sum2: (Int) => (Int) => Int = <function1>

This will just make b an implicit value for the scope of the function body, so you can call methods that expect an implicit Int.

I don’t think you can have implicit arguments for functions since then it is unclear what the function is. Is it Int => Int or () => Int?

The closest I found is:

scala> case class Foo(implicit b: Int) extends (Int => Int) {def apply(a: Int) = a + b}
defined class Foo

scala> implicit val b = 3
b: Int = 3

scala> Foo()
res22: Foo = <function1>

scala> res22(2)
res23: Int = 5

Leave a Comment