关于Play框架代码的困惑 [英] Confusion about Play framework's code
问题描述
我是Scala地区的一个新手,最近,我正在尝试玩Play框架.我使用Play控制台生成了我的第一个应用程序.
I am a very newbie in Scala area, recently, I am trying to play around the Play framework. I used Play console to generate my first application.
然后,我正在阅读代码,但是对于以下代码段,我感到很困惑.
Afterwards, I am reading the code, but for the following code snippets, I just got confused.
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
在文档方面,Action是一个伴随对象,但是伴随对象在大括号后面的语法是什么,就像Java中接口的匿名实现一样,还是在大括号内的内容是apply的替代方法?
In terms of documentation, Action is a companion object, but what is the syntax for a companion object following curly brackets, is it like the anonymous implementation of an interface in Java, or the content inside curly brackets is the override of apply method?
请指教,非常感谢.对于斯卡拉退伍军人这个天真而简单的问题,我们深表歉意.
Please advice, thanks a lot. Sorry for this naive and simple question for Scala veterans.
推荐答案
这是Action.apply
的语法糖. (您不能具有object
的匿名实现,但是如果这是一个trait
,则可能还需要new
).它有一些重载:
This is syntactic sugar for Action.apply
. (You can't have an anonymous implementation of an object
, but you could if this were a trait
, it would also require new
). It has a few overloads:
final def apply[A](bodyParser: BodyParser[A])(block: R[A] => Result): Action[A]
final def apply(block: R[AnyContent] => Result): Action[AnyContent]
final def apply(block: => Result): Action[AnyContent]
您的代码段正在使用这三个代码中的最后一个.顺便说一句,它们都返回实现Action[A]
特征的对象.
Your snippet is using the last of those three. Incidentally, they all return objects implementing the Action[A]
trait.
这篇关于关于Play框架代码的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!