Reputation: 4811
I want to understand how the request variable comes in scope when you do:
def test(): Action = { request =>
}
Could you also bring in other variables in scope? How does this work under the covers in Scala?
In a relation issue, I created my own custom Action and I was confused as to what type it really is?
I did something like the Authentication example:
class AuthenticatedRequest[A](val username: String, request: Request[A]) extends WrappedRequest[A](request)
object Authenticated extends ActionBuilder[AuthenticatedRequest] {
def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[SimpleResult]) = {
request.session.get("username").map { username =>
block(new AuthenticatedRequest(username, request))
} getOrElse {
Future.successful(Forbidden)
}
}
}
Reference: https://www.playframework.com/documentation/2.2.x/ScalaActionsComposition
In my controller I wanted to pass the 'request' as a parameter to a method, but I couldn't figure out what exact type it was. When I tried AuthenticatedRequest it said I have to pass in type parameters.
Upvotes: 0
Views: 50
Reputation: 727
An Action is a function that takes a Request[A] object as argument and returns a Result, and thats the type of your test method. Consider a simple example
type StrLen = String => Int
/**
* A function that returns another function from String to Int
*/
def test2(): StrLen = { someString => someString.length() }
OR
def test2(): StrLen = str: String => { /* body */
str.length()
}
Upvotes: 1