Could someone demystify this code which is part of the zentasks example in the Play20 framework. I\'m curious how this works, granted I\'m new to Scala from Java so a lot of thi
You need to split up the signature a bit. f
is a function that takes a not-yet-computed string => String
and returns another function that accepts a Request[AnyContent]
and returns a result.
The Security.Authenticated
call accepts two parameters lists. One that has username
and onUnauthorized
. The second takes a function accepting the user and returning an action.
The Action.apply
method accepts a function Request[AnyContent] => Result
so, the f is called in 'curried' fashion. That is the first function is called, and then the resulting function is immediately used f(user)(request)
.
Here's the same thing desugared (at least, as best I can) and ugly:
def isAuthenticated(f: => String => Request[AnyContent] => Result) =
Security.Authenticated(username, onUnauthorized) { user: String =>
Action.apply { request: Request[AnyContent] =>
val hiddenTmp: Request[AnyContent] => Result = f(user)
hiddenTemp.apply(request)
}
}
You can see the compiler is doing a bit of work removing type annotations. Hopefully that helps explain how it desugars into raw scala. Essentially, the function does a lot of functional composition.