Scala: def vs val on Play2

I follow the instructions on Play! Framework Essentials, and sometimes I see action in controllers defined with def , and sometimes with val .

I know that def will re-evaluate the expression when called, and val will immediately evaluate the expression, but is there any difference in the context of the controller action?

Here is a sample code:

object Items extends Controller {

  val list = Action { implicit request =>
    ...
  }

  val create = Action { implicit request =>
    ...
  }

  def details(id: Long) = Action { implicit request =>
    ...
  }

  def update(id: Long) = Action { implicit request =>
    ...
  }
}
+4
source share
1 answer

As you said, it defwill re-evaluate the expression every time it is called, and valwill be evaluated when the object is created Items.

, , def, , , , Action, , , ..

, val, def.

0

All Articles