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

2023-12-30

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

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

玩!斯卡拉安全 http://www.playframework.org/documentation/2.0.1/ScalaSecurity

什么是f: User => Request[AnyContent] => Result意思是?乍一看它像是一个返回函数的方法r请求类型;r然后返回一个Result.

这是正确的假设吗?


f: User => Request[AnyContent] => Result 是什么意思?乍一看,它看起来像是一个返回 Request 类型的函数 r 的方法; r 然后返回一个结果。

f返回类型的函数Request[AnyContent] => Result,即一个函数需要Request[AnyContent]并返回一个Result.

换句话说f是一个柯里化函数。你可以称之为f(user)(request)找回一个Result.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

方法参数中多个连续的粗箭头在 Scala 中意味着什么? 的相关文章

随机推荐