In Play2 documentation, I find things like this:
def LoggingAction(f: Request[AnyContent] => Result): Action[AnyContent] = {
Action { request =>
Logger.info("Calling action")
f(request)
}
}
Is it a convention in Scala to use an uppercase first letter for definitions that can be called with { ... } because of curryfication? Or is it just a Play2’s choice?
I’m talking about both LoggingAction and Action.
First, as @pedrofurla points out in his comment, currying has nothing to do with what you’re seeing here. Second, while
LoggingActionandActionbehave similarly from the perspective of an API user, it’s important to distinguish between them.Actionis capitalized by convention because it is the name of a class and a companion object. This may not be obvious from the context since it appears thatActionis being called like a function, but what’s really going on is more subtle. Since theActioncompanion object defines anapplymethod,is treated as shorthand for
That is, what looks like a function invocation is really just an invocation of the
Actionobject’sapplymethod. In cases like this where a method takes a single function as an argument it’s common to use braces:LoggingAction, on the other hand, is a method. Uppercase method names are not conventional and could potentially confuse users of the API. However, it may be appropriate in cases like this one, since it creates consistency throughout the API. Generally, the intent of such usage is to make it clear that the call is creating some thing (here, a logging action) rather than doing something. A rough rule of thumb is that a capital letter indicates a noun, while a lowercase indicates a verb.