Search code examples
scalaplayframeworktwirl

Twirl template default parameter value is not applied


I have a Play 2.5 template which starts from the following declaration:

@(title: String)(content: Html)(menu:Html = HtmlFormat.empty)(implicit request:Request[AnyContent])

So the second parameter is declared having a default value.

Now in the controller I have this action generator:

def document(title:String) = Action.async{implicit request =>
    documentService.findByTitle(title).map{
      case Some(d) => Ok(views.html.document(d))
      case None => Ok(main("No document found")(content = Html("There is no such document")))
    }
  }

So I do not pass the value of the menu parameter to the template invocation and I expect this to compile and work in accordance with the default parameter values semantics, but I am getting this compilation error:

[error] D:\Projects\feed\app\controllers\MainController.scala:28: missing arguments for method apply in class main; 
[error] follow this method with `_' if you want to treat it as a partially applied function 
[error] case None => Ok(main("No document found")(content = Html("There is no such document"))) 
[error]                                          ^ 
[error] one error found 
[error] (compile:compileIncremental) Compilation failed

Could you explain what is wrong here?


Solution

  • Add one more pair of parenthesis.

    Ok(main("No document found")(content = Html("There is no such document")()))
    

    Without last parenthesis - it's just a function that waits for one more argument. You can check type of function you call. I'll show on my examples:

    def foo(a: Int = 3) = 41
    
    val one = foo //wan't compile
    val two: (Int) => Int = foo
    val three: Int = foo()