VM4
VM4

Reputation: 6501

What is this Scala construct doing?

I've been using Play! Framework with Java and would like to try it out with Scala. I've started on a Scala book but the most basic Play! sample has me completely puzzled:

  def index(): Action[AnyContent] = Action { implicit request =>
    Ok(views.html.index())
  }

What Scala construct is Play! using here? I understand that we are defining a function that returns an Action with a generic parameter AnyContent. But the next part has me puzzled. What does the assignment mean in this context?

If I go to definition of Action[AnyContent] it's defined as trait Action[A] extends EssentialAction { ... } If I go to the definition of Action after equals it's defined as:

trait BaseController extends BaseControllerHelpers {
  /**
   * The default ActionBuilder. Used to construct an action, for example:
   *
   * {{{
   *   def foo(query: String) = Action {
   *     Ok
   *   }
   * }}}
   *
   * This is meant to be a replacement for the now-deprecated Action object, and can be used in the same way.
   */
  def Action: ActionBuilder[Request, AnyContent] = controllerComponents.actionBuilder
} 

Note: I'm interested in the Scala construct that's used I don't care what Play! is actually doing here which I kind of understand.

Upvotes: 0

Views: 66

Answers (1)

sachav
sachav

Reputation: 1316

You are essentially calling Action.apply(), which is defined here in ActionBuilder. The first and only parameter of the apply() function being the function request => Ok(...).

Upvotes: 2

Related Questions