Implicit keyword before a parameter in anonymous function in Scala

There are two distinct features here.

First, request isn’t really an argument in a method invocation. It’s the argument of an anonymous function. The anonymous function itself is the argument of the method invocation.

Second, declaring an implicit argument in an anonymous function have the convenience of saving you from “forcing” a val into a implicit:

Action { request =>
  implicit val r = request
  Ok("Got request [" + request + "]")
}

I happen to know this a Play framework code, but I am not sure what are the signatures for Action and Ok. I will guess that they are something like that:

def Action(r:Request => Result):Unit
case class Ok(str:msg)(implicit r:Request)

Again, it’s pure guessing for illustrative purposes only.

Leave a Comment