gpt4 book ai didi

scala - 方法参数中多个连续的粗箭头在 Scala 中意味着什么?

转载 作者:行者123 更新时间:2023-12-03 03:43:59 26 4
gpt4 key购买 nike

我知道一个方法可以有这样的代码:

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))
}
}

Play! Scala Security

f: User => Request[AnyContent] => Result 是什么意思?乍一看,它看起来像是一个返回 Request 类型的函数 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 返回 Request[AnyContent] => Result 类型的函数,即采用 Request[AnyContent] 并返回的函数结果

换句话说,f 是一个柯里化(Currying)函数。您可以将其称为 f(user)(request) 以获取 Result

关于scala - 方法参数中多个连续的粗箭头在 Scala 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com