What parts of a Java application should be written in Scala?

I am writing a Java application using Struts 2, but now I would like to make it a hybrid of Java and Scala. I don’t have much experience working with Scala, but I studied Haskell several years ago in college - I really liked the functional programmed paradigm, but, of course, in the class we were presented only problems that were highly suitable for a functional solution! In the real world, I think some code is better for the imperative style, and I want to continue using Java for this (I know that Scala supports the imperative syntax, but I'm not ready to go in the direction of a pure Scala project for now).

In a hybrid project, how to decide what to encode in Java and what to encode in Scala?

+6
java scala interop
source share
4 answers

Ask these questions about your project: "What operations require side effects?" and "What functionality is already well covered by Java libraries?" Then we implement the rest in Scala.

However, I would caution that hybrid projects are inherently more complex than stand-alone projects, as you need to use multiple languages ​​/ environments. Therefore, given that you do not have much experience with Scala, I would recommend that you first play with some toy projects, perhaps a subset of your overall goal. It will also give you a sense of where the break should occur.

+3
source share

Two things:

  • 99% of Java code can be expressed in Scala
  • You can write projects that support Java + Scala mixed compilation. Your Scala code can call your Java code, and your Java code can call your Scala code. (If you want to do the latter, I suggest defining an interface in Java and then just implementing it in Scala. Otherwise, calling Scala code with Java may be a little ugly.)

So the answer is: whatever parts you want. Your Scala code should not be purely functional. Your Scala code may call Java libraries. So almost any part that you could write in Java, you could also write in Scala.

Now a few more practical considerations. When using Scala for the first time, some people choose relatively isolated, non-mission parts of their program to write to Scala. Unit tests are a good candidate if you like this approach.

If you are familiar with Java and have learned Haskell in the past, I suggest considering Scala as "the best Java." Essentially, Scala compiles into JVM bytecode, which is very, very similar to what Java produces. The only difference is that Scala is more “productive”: it generates more bytecode per line of code than Java. Scala has a lot in common with Haskell (first-class functions, for -an understanding are similar to Haskell do-notation, a limited type of output), but it is also very different (it is not lazy by default, it is not clean). So you can use some of your ideas from Haskell to inspire your Scala style, but “under the hood” is all Java bytecode.

+10
source share

In the spirit of your question, I recommend that you write in Scala any code that involves heavy manipulation of collections or XML processing.

Collection library

Scala is Scala's most functional feature, and through its use you will get an excellent LoC abbreviation. Yes, there are Java alternatives such as the Google collection library, but you asked what you should write in Scala. :-)

Scala also has built-in XML processing. It may be difficult for you to find the transition if you try to take the DOM code and make it work with Scala. But if you instead try to approach Scala's problem and perspective and write it from scratch for Scala, you will make a profit.

I would recommend using Actors, but I'm not sure how well you can integrate this with Struts 2 code in Java. But if you have parallel code, give the Scala Actors a thought.

+6
source share

This may sound silly, but why not write your entire project in Scala? This is a great language that is much more expressive than Java, while maintaining binary-compatible access to existing Java libraries.

+5
source share