I have a IsAuthenticated method which has a complex argument type (I copied it from play2’s zentasks example):
def IsAuthenticated(f: => String => Request[AnyContent] => Result): Action[(Action[AnyContent], AnyContent)] =
Security.Authenticated(username, onUnauthorized) { userId =>
Action { implicit request =>
val email = request.session("user.email")
f(email)(request)
}
}
In order to use it, my action is:
def delete(id:String) = IsAuthenticated { email => request =>
...
}
You can see I have to declare email event if I don’t need to use it. I can use _ instead:
def delete(id:String) = IsAuthenticated { _ => _ =>
...
}
But _ => _ => is still boring.
How to refactor the method to let it’s usage simpler? e.g. If I don’t need email and request, I can:
def delete(id:String) = IsAuthenticated {
...
}
If I just need request, I can:
def delete(id:String) = IsAuthenticated { request =>
...
}
If I need email, then I declare them all:
def delete(id:String) = IsAuthenticated { email => request =>
...
}
You can overload the
IsAuthenticatedto provide the different flavours you need. For example:You can then use it as follows: