我知道一个方法可以有这样的代码:
def m(p1:Int => Int) ...
这意味着此方法采用返回 Int 的函数 p1
但是在浏览 Play!框架代码我发现了一个具有难以理解的方法的特征:
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))
}
}
是什么f: User => Request[AnyContent] => Result
意思?乍一看,它看起来像一个返回r
Request 类型函数的方法;r
然后返回一个Result
.
这是正确的假设吗?