Playback pattern only works when called with parentheses

I have the following code in my controller Application:

  def index = Action {
    var ticketsAvailable = 1
    Ok(views.html.index)
  }

When I try to run the application, I get this compilation error:

Cannot write an instance of views.html.index.type to HTTP response. Try to define a Writeable[views.html.index.type]

In line

Ok(views.html.index) 

However, when I add the brackets (as shown below), the error disappears:

Ok(views.html.index()) 

Why? I thought that in Scala parentheses are optional if there are no arguments.

+4
source share
1 answer

It’s really useful to look at the generated Scala code for a simple Twirl template (Twirl - a playback template engine) called index.scala.html:

@()

<h1>Hello, world</h1>

Play Scala target/scala-2.11/twirl/main/views/html/index.template.scala , , ( ), :

package views.html
import play.twirl.api._

object index_Scope0 {
  import models._
  import controllers._
  import play.api.i18n._
  import views.html._
  import play.api.templates.PlayMagic._
  import play.api.mvc._
  import play.api.data._

  class index
    extends BaseScalaTemplate[play.twirl.api.HtmlFormat.Appendable, Format[play.twirl.api.HtmlFormat.Appendable]](play.twirl.api.HtmlFormat)
    with play.twirl.api.Template0[play.twirl.api.HtmlFormat.Appendable] {

    def apply(): play.twirl.api.HtmlFormat.Appendable = {
      _display_ {
        {
          Seq[Any](format.raw(""""""), format.raw("""<h1>hello, world</h1>"""))
        }
      }
    }

    def render(): play.twirl.api.HtmlFormat.Appendable = apply()
    def f: (() => play.twirl.api.HtmlFormat.Appendable) = () => apply()
    def ref: this.type = this
  }
}

object index extends index_Scope0.index

, views.html.index - , views.html.index.type, , , . , , views.html.index_Scope0.index, apply(), Html (, HtmlFormat.Appendable, .)

"" Scala, Scala ( "de-sugars" ), apply() ( case new.) , parens, Html, Twirl. , , Play , HTTP.

+7

All Articles