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))
}
}
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?
freturns a function of typeRequest[AnyContent] => Result, i.e. a function that takes aRequest[AnyContent]and returns aResult.In other words
fis a curried function. You could call it asf(user)(request)to get back aResult.