Java, Play2.0, ')' Expected but '=' game found

I am trying to create a " General Templates Usage Examples " tag in a play2.0 doc.

@(level: String = "error")(body: (String) => Html)

@level match {

  case "success" => {
    <p class="success">
      @body("green")
    </p>
  }

  case "warning" => {
    <p class="warning">
      @body("orange")
    </p>
  }

  case "error" => {
    <p class="error">
      @body("red")
    </p>
  }

}

      

then refresh the page http: // localhost: 9000 , get the error:

')' expected but '=' found.
In foo/app/views/tags/notice.scala.html at line 4.
1#{extends 'main.html' /}
2#{set title:'notice.scala.html' /}
3
4@(level: String="error")(body: (String) => Html) 
5
6@level match {
7    
8  case "success" => {

      

since i am new to both play2.0 and scala, the cloud is someone telling me why?

+3


source to share


2 answers


It doesn't really make sense to have a default argument in its own argument group:

@(level: String = "error")(body: (String) => Html)

      

Note that the "moreScripts and moreStyles" example in Scala's common use case templates sets the default argument to another argument:

@(title: String, scripts: Html = Html(""))(content: Html)

      



You can do the same:

@(body: (String) => Html, level: String = "error")

      

Side note: It's not a good idea to rely on strings to distinguish between success / warning / error. Strings are fragile and prone to typos, which confuses errors in annoying ways. Instead, look for a datatype, or create your own to represent it: thus, typos become compiler errors.

class ResultType
case object Success                 extends ResultType
case class Warning(message: String) extends ResultType
case class Error(message: String)   extends ResultType

      

+2


source


I think you should try



@(level: (String) => "error")(body: (String) => Html)

@level match {

  case "success" => {
    <p class="success">
      @body("green")
    </p>
  }

  case "warning" => {
    <p class="warning">
      @body("orange")
    </p>
  }

  case "error" => {
    <p class="error">
      @body("red")
    </p>
  }

}

      

-2


source







All Articles