首页 > 解决方案 > 这个 Scala 结构在做什么?

问题描述

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

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

Scala 的构造是 Play!在这里使用?我知道我们正在定义一个返回Action带有泛型参数的函数AnyContent。但接下来的部分让我感到困惑。在这种情况下,分配意味着什么?

如果我转到 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 构造感兴趣,我不在乎 Play!实际上是在这里做的,我有点理解。

标签: scalaplayframework

解决方案


您实际上是在调用Action.apply(),它在此处ActionBuilder定义。函数的第一个也是唯一的参数是apply()function request => Ok(...)


推荐阅读