What does the word “Action” do in a Scala function definition using the Play framework?

后端 未结 3 629
你的背包
你的背包 2021-02-02 00:45

I am developing Play application and I\'ve just started with Scala. I see that there is this word Action after the equals sign in the function below and before curl

3条回答
  •  失恋的感觉
    2021-02-02 01:21

    Its the Action being called on with an expression block as argument. (The apply method is used under the hood).

    Action.apply({
      Ok("Hello world")
    })
    

    A simple example (from here) is as follows (look at comments in code):

    case class Logging[A](action: Action[A]) extends Action[A] {
    
      def apply(request: Request[A]): Result = {// apply method which is called on expression
        Logger.info("Calling action")
        action(request) // action being called on further with the request provided to Logging Action
      }
    
      lazy val parser = action.parser
    }
    

    Now you can use it to wrap any other action value:

    def index = Logging { // Expression argument starts
      Action { // Action argument (goes under request)
        Ok("Hello World")
      }
    }
    

    Also, the case you mentioned for def index = { is actually returning Unit like: def index: Unit = {.

提交回复
热议问题