方法参数中多个连续的粗箭头在Scala中意味着什么? [英] What do multiple, consecutive fat arrows in method parameters mean in Scala?
问题描述
我了解一种方法可以具有如下代码:
I understand that a method can have code like this:
def m(p1:Int => Int) ...
这意味着该方法采用的函数p1返回一个Int
Which means this method takes a function p1 that returns an Int
但是在浏览Play时!框架代码我发现了一种难以理解的方法的特征:
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))
}
}
f: User => Request[AnyContent] => Result
是什么意思?乍一看,它看起来像一个返回请求类型为r
的函数的方法. r
然后返回Result
.
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
.
这是正确的假设吗?
推荐答案
f:User => Request [AnyContent] =>结果是什么意思?乍一看,它看起来像一个返回Request类型的函数r的方法;然后r返回结果.
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.
f
返回类型为Request[AnyContent] => Result
的函数,即采用Request[AnyContent]
并返回Result
的函数.
f
returns a function of type Request[AnyContent] => Result
, i.e. a function that takes a Request[AnyContent]
and returns a Result
.
换句话说,f
是一个咖喱函数.您可以将其称为f(user)(request)
以获取Result
.
In other words f
is a curried function. You could call it as f(user)(request)
to get back a Result
.
这篇关于方法参数中多个连续的粗箭头在Scala中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!