Play 2.0+Java vs. Play 2.0+Scala?

I just finished a prototype using Play 2.0 with Java and now am considering to learn Scala just so I can switch to it for further development.

It’s not just the usual Java vs. Scala discussion – the problem as I see it with the Play framework is that it forces Scala idioms onto Java. An example from the documentation about calling multiple web services:

public static Result feedComments(String feedUrl) {
  return async(
    WS.url(feedUrl).get().flatMap(
      new Function<WS.Response, Promise<Result>>() {
        public Promise<Result> apply(WS.Response response) {
          return WS.url(response.asJson().findPath("commentsUrl").get().map(
            new Function<WS.Response, Result>() {
              public Result apply(WS.Response response) {
                return ok("Number of comments: " + response.asJson().findPath("count"));
              }
            }
          );
        }
      }
    )
  );
}

It works but doesn’t look like conventional Java. Those parentheses look really scary. Even Eclipse gets confused and never knows what generics I need or want to use – I always have to choose them manually.

Also note that in the documentation they made this look pretty by removing @Override annotations, using just two spaces for indentation and overall choosing a very simple example without validation or error recovery so they don’t use too many lines. I’m not even sure you could configure a code formatter to output it like this without messing up other code completely.

In practice I ended up with an unreadable block of a horrible Java-Scala-abomination just for getting some data from another service.

Unfortunately I can’t find any example of combining responses in Scala. At least calling single web services in Scala looks much shorter and easier to read.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)