In Play scala html template, one can specify
@(title: String)(content: Html)
or
@(title: String)(content: => Html)
What is the difference?
Writing parameter: => Html is called a 'by name parameter'.
Example:
def x = { println("executing x") 1 + 2 } def y(x:Int) = { println("in method y") println("value of x: " + x) } def z(x: => Int) = { println("in method z") println("value of x: " + x) } y(x) //results in: //executing x //in method y //value of x: 3 z(x) //results in: //in method z //executing x //value of x: 3 By name parameters are executed when used. The problem with this is that they can be evaluated more than once.
A good example would be an if statement. Let's say if was a method like this:
def if(condition:Boolean, then: => String, else: => String) It would be a waste to evaluate both then and else before the method is executed. We know only one of the expressions will be executed, the condition is either true or false. That's the reason when and else are defined as 'by name' parameters.
This concept is explained in the Scala course: https://www.coursera.org/course/progfun