简体   繁体   中英

Play 2.0+Java vs. Play 2.0+Scala?

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?

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.

AFAIK, it is better to go with Scala, because it offers more functionnality.

For instance (correct me if I'm wrong), you can directly using the Iteratees with Scala (aka reactive programming), but I don't know how to achieve that with Java.

Also, the Java API of Play is an upper layer in the Play architecture, the core is written with Scala; so if you want to lean what's happening inside, go with Scala.

And also note that you can mix Java and Scala in the same Play project, so you can move smoothly to Scala.

I was new to Play and just wrote a small-mid sized Project with the Java side of it.

It works and is powerful, but a lot of Features just seem to be very Scala centric and the Java support is sub-par. The Java API Doc is virtually non existent, which takes away some comfort in the IDE (IntelliJ - I recommend it). The integration in the Java world is not very "natural" (SBT..), It does not build as a war or maven module (but there seem to be plugins to do this). You kind of loose the benefits of the existing Java tooling.

The concepts which differentiate Play from the other Frameworks almost all seem to be scala native and Java second.

I stumbled onto some bugs and quirks and I always would end up in Scala code I had to read and understand to fix or work around it. (Actions, RequestBody stuff, Async, routes, template implicits and so on)

So the Java side of things just feels very second tier to me, although they are clearly making an effort for them to be equally good.

Now, after the Project, I would say it is very usable and would even use it again (in Java).

What advantages do I have if I start to learn and use scala in a play 2.0 application?

As djangofan commented, when you google for Play help, you get examples in Scala.

Is there anything that I cannot do with java over scala in a Play 2.0 application?

However, I haven't yet (still a beginner) found an equivalent of Ebean for Scala. The Scala examples I've found all use direct SQL for persistence. Where's the database abastraction?

Since I haven't found a database abstraction for Play with Scala, I'm continuing with Java...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM