I'm a bit stuck with implicit parameters of anonymous functions. Hope somebody'll point me the right direction. Here is what I have. Two files: Main.scala and Foo.scala:
// Foo.scala
trait Fun[-A, +B] extends (A => B)
trait ImplicitString[+B] {
def withString(block: String => B)(implicit s: String): B = block(s)
}
object FooFun extends Fun[String, String] with ImplicitString[String] {
def apply(x: String): String = withString { implicit s =>
x + s
}
}
And
// Main.scala
object Main extends App {
implicit val s = "it works!"
println(FooFun("Test:"))
}
I'm expecting to see Test: it works! printed. But I got a compilation error:
$ scalac Main.scala Service.scala
Service.scala:8: error: could not find implicit value for parameter s: String
def apply(x: String): String = withString { implicit s =>
^
one error found
Am I missing something?
UPDATE:
Looks like I should have imported my implicit val like this:
// Foo.scala
import Main._
...
This works fine.
(A => B)is nothing butFunction[-A, +B], so yourtrait Funreally isn't needed.