关于 Play 框架和代码的困惑 [英] Confusion about Play framework's code

查看:55
本文介绍了关于 Play 框架和代码的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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?

请指教,非常感谢.对于 Scala 老手提出的这个幼稚而简单的问题,我深表歉意.

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] trait 的对象.

Your snippet is using the last of those three. Incidentally, they all return objects implementing the Action[A] trait.

这篇关于关于 Play 框架和代码的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆