1

I am pretty new to Scala and I am learning Play as well. I see the following construct used in Play

def list = Action {
    val products = Product.findAll
    Ok(views.html.products.list(products))
}

I am confused as to what

Action {}

does. Is Action the returned value of the method? What is this construct called if I want to know more about it?

denniss
  • 17,229
  • 26
  • 92
  • 141

1 Answers1

2

This construction called factory method enhanced via scala apply sugar

Action in this reference is the companion object, which could be called singleton, but in fact along with very specific singleton type Action$ it methods reflected as static methods of Action.

As we can read object Action extends ActionBuilder[Request] which have plenty of apply methods constructing values of Action type.

Curly braces here presents nullary function which is null-parameter closure and often named so in different languages like ruby or groovy. It's just multiline block of code which produces something at the end.

Community
  • 1
  • 1
Odomontois
  • 15,918
  • 2
  • 36
  • 71