0

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.

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • I see no currying going on this code. Currying is turning a multi-argument function into a series of partially applied functions. For example (A,B,C) => D into A => B => C => D. – pedrofurla Oct 27 '12 at 03:20

1 Answers1

2

First, as @pedrofurla points out in his comment, currying has nothing to do with what you're seeing here. Second, while LoggingAction and Action behave similarly from the perspective of an API user, it's important to distinguish between them.

Action is 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 that Action is being called like a function, but what's really going on is more subtle. Since the Action companion object defines an apply method,

Action(arg)

is treated as shorthand for

Action.apply(arg)

That is, what looks like a function invocation is really just an invocation of the Action object's apply method. In cases like this where a method takes a single function as an argument it's common to use braces:

Action apply { request =>
   // ...
}

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.

Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
  • @pedrofurla No, it wouldn't since `LoggingAction` is a method rather than an object. I'm not sure what prompted this comment, but I guess I need to clarify my answer. – Aaron Novstrup Oct 27 '12 at 04:10