What Are The Benefits Of Scala? [closed]

2020-05-11 10:53发布

问题:

I am a Java developer and I want to know how I can use Scala in my Java programs?

回答1:

Go read Daniel Spiewak's excellent blog series about Scala. With Scala you can keep:

  • all your Java libraries
  • all the advantages of running on a JVM (ubiquity, administrative tools, profiling, garbage collection etc)

But you can write Scala code:

  • more concise and clear than Java (especially using more functional style, such as in the collections library)
  • it has closures and functions as part of the language
  • it has operator overloading (from the perspective of usage)
  • it has mixins (i.e. interfaces which contain implementation)


回答2:

Also, take a look at this recent news item post on Scala's site: "Research: Programming Style and Productivity".

In his paper, Gilles Dubochet, describes how he investigated two aspects of programming style using eye movement tracking. He found that it is, on average, 30% faster to comprehend algorithms that use for-comprehensions and maps, as in Scala, rather than those with the iterative while-loops of Java.

And another key quote from the news item:

Alex McGuire, who writes mission critical projects in Scala for power trading companies, says of Scala "The conciseness means I can see more of a program on one screen. You can get a much better overview. When I have some mathematical model to write with Java I have to keep two models in my head, the mathematical model itself and the second a model of how to implement it in Java. With Scala one model, the mathematical one, will do. Much more productive.”

You an read the rest of the post and other linked items there.



回答3:

UPDATED 2019

I can name some simple points in plain language from my limited experience:

  1. Properties. C++ and Java had this notion of a public getter/setter function "property" wrapped around an internal class variable which led to large amounts of boilerplate code. C# formalized this as a real language feature and reduced much of the boilerplate in C# 3.0 with auto-implemented properties. Scala classes define trivial properties simply as regular read only vals or read/write vars. The class may later choose to replace those with get or get/set methods without affecting client code. For this, Scala provides the most elegant solution with the least language features and complexity.

  2. Arrays use regular generics. In Java/C#, int[] is redundant and confusing vs List<int> or List<Int>. Worse, in Java, List<Int> has lots of runtime overhead, so many developers have to know to use int[]. Scala avoids that problem. Also, in Java/C#, arrays support (covariant) casting, which was a mistake, that they now can't fix because of legacy concerns.

  3. Scala has better support for immutability. val is a basic language feature.

  4. Scala lets if blocks, for-yield loops, and code in braces return a value. This is very elegant in many situations. A very small plus is that this eliminates the need for a separate ternary operator.

  5. Scala has singleton objects rather than C++/Java/C# class static. This is a cleaner solution.

  6. Pattern matching. Object unpacking. Very nice in a large numbers of situations.

  7. Native tuples.

  8. "case classes" which are what most other languages would call record types or named tuples.

  9. Fancier standard library with more elegant collections.

  10. Multi-line strings. String interpolation formatting.

  11. Optional semi-colons.

Cons.

  1. Java has caught up a lot. Java 8 was first released in 2014, but it took several years for older Java versions to be phased out and the new Java 8 features to be fully used across the Java ecosystem. Now, lambdas and closures and basic functional collections, with support for filter/map/fold are quite standard for the Java ecosystem. More recently, Java has added basic var local variable type inference and has multi-line strings and switch expressions in release builds preview mode.

  2. Scala is complicated. I'd highlight features like implicits to be inherently confusing.

  3. Scala has minimal backward compatibility. Scala 2.10 artifacts are incompatible with Scala 2.11.

  4. Building a Java API for other JVM-language developers like Scala or Clojure or Kotlin is normal, well supported and accepted. You generally don't want to build APIs in Scala that cater to non-Scala developers.



回答4:

I am not sure you can easily use Scala in your Java programs, as in "call a Scala class from a Java class".

You can try, following the article "Mixing Java and Scala".
Relevant extracts:

The problem is that the Java and Scala compilation steps are separate: you can't compile both Java and Scala files in one go.
If none of your Java files reference any Scala classes you can first compile all your Java classes, then compile your Scala classes.
Or, if none of your Scala files reference any Java classes you can do it the other way around.
But if you want your Java classes to have access to your Scala classes and also have Scala classes have access to your Java classes, that's a problem.

Scala code can easily call directly into Java code, but sometimes calling Scala code from Java code is trickier, since the translation from Scala into bytecode is not quite as straightforward as for Java:
sometimes the Scala compiler adds characters to symbols or makes other changes that must be explicitly handled when calling from Java.
But a Scala class can implement a Java interface, and an instance of that class can be passed to a Java method expecting an instance of the interface.
The Java class then calls the interface methods on that instance exactly as if it were a Java class instance.

The opposite is possible, of course, as described in Roundup: Scala for Java Refugees, from Daniel Spiewak.



回答5:

Scala or Java:

Pros:

  • Scala supports both functional and imperative OO programming styles and it advocates that both models are not conflicting with each other but yet they are orthogonal and can complement each other. Scala doesn't require or force the programmer to use a particular style, but usually the standard is to use functional style with immutable variables when appropriate (there are several benefits of using the functional approach such as concise and short syntax and using pure functions usually reduces the amount of non-determinism and side-effects from the code), while resorting to imperative programming when the code would look simpler or more understandable.
  • Scala doesn't require ; at the end of each line having it optional which leads to cleaner code
  • In Scala functions are first class cititzens
  • Scala supports some advanced features which are directly built in the language such as: Currying, Closures, Higher order functions, pattern matching, Higher Kinded Types, Monads, implicit params.
  • Scala can interact very well with Java and both can coexist. It is possible to use java libraries directly inside Scala code invoking Java classes from scala code.
  • Has Tuples built in the language which makes life easier in several scenarios
  • Supports operator overloading
  • has a rich Ecosystem and some popular open source projects in Apache are based on it.
  • Async and Non-blocking code is very easy to write with Scala Futures
  • Scala supports the Actor model using Akka which can be highly efficient and scalable when running distributed applications in multi-threaded and parallel business use cases (Enforce encapsulation without resorting to locks, State of actors is local and not shared, changes and data is propagated via message)
  • Code tends to be shorter if compared to Java (might not be always the case)

Cons:

  • Steep learning curve if compared to Java and other languages, requires more time in general from the learner to understand all the concepts clearly. Has many features
  • It is not as well established as Java in the market since it was invented later so Java in overall is more mature and more battle-tested.
  • Scala opens too many doors. It allows a lot of complex syntax that if used in a irresponsible way might lead to code that is hard to understand. Abusing things such as operator overloading, implicit params and other constructs can be counter-productive and might ruin code legibility.
  • Java is also evolving and still getting better with newer versions (such as with JDK 9 modules)