Daniel Wu
Daniel Wu

Reputation: 6003

How to understand type in the function body instead of in the function parameter list

In file of Action.scala in play framework, there is a following function:

  final def apply[A](bodyParser: BodyParser[A])
         (block: R[A] => Result): Action[A] 
         = async(bodyParser) { req: R[A] =>
    Future.successful(block(req))
  }

there is a section as below:

  { req: R[A] =>
        Future.successful(block(req))
      }

If in a function parameter list: that means req is a type of

   R[A] =>Future.successful(block(req))

But it used req in block(req), so looks like recursive. And now it's not in parameter list but in a body, how to understand it?

Upvotes: 1

Views: 64

Answers (1)

Siphor
Siphor

Reputation: 2544

No, req is of type R[A].
req: R[A] =>Future.successful(block(req)) is a function that takes a object of type R[A] and returns Future.successful(block(req))

Example:

def fun(param:Int=>String)=param(9)

declares a function that takes a function as a parameter

def fun2 = fun({i:Int=>i.toString})

gives fun a implemented function

Upvotes: 3

Related Questions