scala - 这个 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!实际上是在这里做的,我有点理解。
解决方案
您实际上是在调用Action.apply()
,它在此处ActionBuilder
定义。函数的第一个也是唯一的参数是apply()
function request => Ok(...)
。
推荐阅读
- windows - 向循环添加函数
- elasticsearch - 如何仅根据词频获取文档?
- android - Android Fragment:进入和返回的不同动画
- java - 如何修复错误:未找到 ID 为“com.google.gms:google-services:4.2.0”的插件
- r - 提取以特定预定义字符串正则表达式开头的行?
- python - 我不能在 WSL Ubuntu 上使用 pip
- c# - Mongo DB C# 驱动程序 - 如何定义没有属性的复合 ID?
- npm - npm 组存储库在 nexus 3 中的意外包解析行为
- django - DJANGO 如何进行可以在两个值之间进行过滤的 Q 查询?
- docker - 我尝试在 docker 中部署 gRPC (go) 服务器并在本地端口中公开端口,但端口绑定不起作用