Alex Rivera | Logout

What do multiple, consecutive fat arrows in method parameters mean in Scala?

Asked 2012-12-23T17:40:28.093
12

I understand that a method can have code like this:

def m(p1:Int => Int) ...

Which means this method takes a function p1 that returns an Int

But while browsing the Play! framework code i found a trait with indecipherable methods:

trait Secured {

  def username(request: RequestHeader) = request.session.get(Security.username)

  def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Auth.login)

  def withAuth(f: => String => Request[AnyContent] => Result) = {
    Security.Authenticated(username, onUnauthorized) { user =>
      Action(request => f(user)(request))
    }
  }

  /**
   * This method shows how you could wrap the withAuth method to also fetch your user
   * You will need to implement UserDAO.findOneByUsername
   */
  def withUser(f: User => Request[AnyContent] => Result) = withAuth { username => implicit request =>
    UserDAO.findOneByUsername(username).map { user =>
      f(user)(request)
    }.getOrElse(onUnauthorized(request))
  }
}

Play! Scala Security

What does the f: User => Request[AnyContent] => Result mean? At first glance it looks like a method that returns a function r of type Request; r then returns a Result.

Is this the right assumption?

Edit
Report

1 Answer

1

def withAuth(f: => String => Request[AnyContent] => Result) means, that f is a by-name parameter, and you can write something like this:

withAuth {
  logger.info("Here we go")
  ...
  chooseTheAction("list")
}

where chooseTheAction takes a String and returns a function performing a request, Request[AnyContent] => Result

answered 2012-12-23T22:30:20.217

Your Answer