Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 508
dreampowder Avatar asked Sep 23 '12 12:09

dreampowder


People also ask

What is play framework in Scala?

Play Framework is an open-source web application framework which follows the model–view–controller (MVC) architectural pattern. It is written in Scala and usable from other programming languages that are compiled to JVM bytecode, e.g. Java.

Is Play framework still used?

Play is rock-solid and used by hundreds of thousands of Java and Scala developers every month. Play is still extremely relevant to today's application and web development and has a passionate and very capable community around it ensuring that it has many good years left.

Why play framework is used?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.


1 Answers

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.

like image 183
kapex Avatar answered Oct 06 '22 20:10

kapex