class 扩展参数中的函数定义?
Function definition in parameter of class extension?
在this part of Play Framework documentation中,我找到了下面一行代码:
object Authenticated extends AuthenticatedBuilder(req => getUserFromRequest(req))
让我困惑的部分是eq => getUserFromRequest(req))
。为什么超类的参数中有函数定义?难道它不应该只保存超类中应该由子类继承的参数吗?
在 Scala 中,函数是 first-class 个元素,这意味着它们可以作为参数传递并作为函数结果返回。
从source file我们看到AuthenticatedBuilder
需要两个构造函数参数,它们都是函数。
class AuthenticatedBuilder[U](userinfo : RequestHeader => Option[U]
,onUnauthorized : RequestHeader => Result =
_ => Unauthorized(views.html.defaultpages.unauthorized())
) extends ActionBuilder[...] { ...
在您引用的示例中,仅提供了一个参数,因为第二个参数具有默认值。
在this part of Play Framework documentation中,我找到了下面一行代码:
object Authenticated extends AuthenticatedBuilder(req => getUserFromRequest(req))
让我困惑的部分是eq => getUserFromRequest(req))
。为什么超类的参数中有函数定义?难道它不应该只保存超类中应该由子类继承的参数吗?
在 Scala 中,函数是 first-class 个元素,这意味着它们可以作为参数传递并作为函数结果返回。
从source file我们看到AuthenticatedBuilder
需要两个构造函数参数,它们都是函数。
class AuthenticatedBuilder[U](userinfo : RequestHeader => Option[U]
,onUnauthorized : RequestHeader => Result =
_ => Unauthorized(views.html.defaultpages.unauthorized())
) extends ActionBuilder[...] { ...
在您引用的示例中,仅提供了一个参数,因为第二个参数具有默认值。