这个 Scala 结构在做什么?

What is this Scala construct doing?

我一直在使用 Play! Java 的框架,想用 Scala 试试。 我已经开始阅读 Scala 书籍,但最基本的 Play!样品让我完全困惑:

  def index(): Action[AnyContent] = Action { implicit request =>
    Ok(views.html.index())
  }

Play 是什么 Scala 结构! using here? 我知道我们正在定义一个 returns 一个带有通用参数 AnyContentAction 的函数。但是接下来的部分让我感到困惑。在这种情况下,赋值是什么意思?

如果我转到 Action[AnyContent] 的定义,它被定义为 trait Action[A] extends EssentialAction { ... } 如果我在 equals 之后转到 Action 的定义,它被定义为:

trait BaseController extends BaseControllerHelpers {
  /**
   * The default ActionBuilder. Used to construct an action, for example:
   *
   * {{{
   *   def foo(query: String) = Action {
   *     Ok
   *   }
   * }}}
   *
   * This is meant to be a replacement for the now-deprecated Action object, and can be used in the same way.
   */
  def Action: ActionBuilder[Request, AnyContent] = controllerComponents.actionBuilder
} 

注意:我对使用的 Scala 构造很感兴趣我不在乎玩什么!实际上是在这里做的,我有点理解。

您实质上是在调用 Action.apply(),它在 ActionBuilder 中定义 hereapply() 函数的第一个也是唯一一个参数是函数 request => Ok(...).