Java – Play 2.0+Java vs. Play 2.0+Scala

javaplayframework-2.0scala

I am thinking about migrating to play 2.0 after play 1.2. One thing that bothers me is that people say Scala is more "preferred" for a play 2.0 application. I know the differences over 1.2 and 2.0, but I'm unsure if there are differences between Play 2.0 with Java and Play 2.0 with Scala

So there are questions in my mind:

  • Is there anything that I cannot do with java over scala in a Play
    2.0 application?
  • What advantages do I have if I start to learn and use scala in a
    play 2.0 application?

Best Answer

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.