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 things are tough to wrap my head around.
def IsAuthenticated(f: => String => Request[AnyContent] => Result) =
Security.Authenticated(username, onUnauthorized) { user =>
Action(request => f(user)(request))
}
You need to split up the signature a bit.
fis a function that takes a not-yet-computed string=> Stringand returns another function that accepts aRequest[AnyContent]and returns a result.The
Security.Authenticatedcall accepts two parameters lists. One that hasusernameandonUnauthorized. The second takes a function accepting the user and returning an action.The
Action.applymethod accepts a functionRequest[AnyContent] => Resultso, 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:
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.