Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Play2.0,')' expected but '=' found play

I follow the "Common template use cases" in play2.0 document, when I try to create a tag.

@(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 an error say:

')' 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 newbie in both play2.0 and scala, cloud someone tell me why?

like image 493
Chan Avatar asked Jun 01 '26 06:06

Chan


1 Answers

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

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

Notice how the example "moreScripts and moreStyles" at Scala templates common use cases puts the defaulted argument with another argument:

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

You can do the same:

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

Side note: it isn't a very good idea to rely on Strings to distinguish between success/warning/error. Strings are fragile and subject to typos, which will hide bugs in annoying ways. Instead, look for a data type, or create your own, to represent this: that way typos become a compiler error.

class ResultType
case object Success                 extends ResultType
case class Warning(message: String) extends ResultType
case class Error(message: String)   extends ResultType
like image 143
Dan Burton Avatar answered Jun 02 '26 18:06

Dan Burton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!