How can I mark an argument to an anonymous n-ary function implicit? For example, consider the following:
def foo(f: (String, Int) => String): String = f("cat", 2)
def bar(s: String)(implicit i: Int): String = s * i
foo { implicit (s, i) =>
bar(s)
}
The last line doesn't compile, reporting the following error:
test_tuple_implicits.scala:7: error: expected start of definition
foo { implicit (s, i) =>
^
one error found
This is strange to me because I know it's possible to mark an argument as an implicit for unary functions, such as when using Play web framework:
Action { implicit request =>
Ok("Got request [" + request + "]")
}
I've also tried marking each value implicit foo { (implicit i, implicit s) =>
with no luck. Search engines have not been very helpful either, but I'm not sure I have the right keywords.
EDIT: It seems like I can make this work by editing the definitions like this:
def foo(f: String => Int => String): String = f("cat")(1)
def bar(s: String)(implicit i: Int) = s * i
foo { s => implicit i =>
bar(s)
}
I'm wondering if there is a cleaner solution though, and in particular a way to mark two values implicit at once (like implicit a, b
).