Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Why Scala?

Why Scala?

This item in japanese

Before answering the question of "Why Scala?", we first need to answer the question "what is Scala?" From the Scala website, the following overview can be found:

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.
  • Scala is object-oriented: Scala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits. Class abstractions are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.
  • Scala is functional: Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala's case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages.
  • Scala is statically typed: Scala is equipped with an expressive type system that enforces statically that abstractions are used in a safe and coherent manner.
  • Scala is extensible: The design of Scala acknowledges the fact that in practice, the development of domain-specific applications often requires domain-specific language extensions. Scala provides a unique combination of language mechanisms that make it easy to smoothly add new language constructs in form of libraries:
    • any method may be used as an infix or postfix operator, and
    • closures are constructed automatically depending on the expected type (target typing).
    A joint use of both features facilitates the definition of new statements without extending the syntax and without using macro-like meta-programming facilities.
  • Scala interoperates with Java and .NET: Scala is designed to interoperate well with popular programming environments like the Java 2 Runtime Environment (JRE) and the .NET Framework (CLR). In particular, the interaction with mainstream object-oriented languages like Java and C# is as smooth as possible. Scala has the same compilation model (separate compilation, dynamic class loading) like Java and C# and allows access to thousands of high-quality libraries.

For some developers, these incentives will be enough to lure you away from Java into the Scala world. But for others, they provide no additional benifits for the day-to-day programming that is currently being performed in Java.

So, once again, "why Scala?"

In a post titled "Scala: The best of both Ruby and Java", Ian describes that it might not be a choice between Java and Scala, but instead the choice to use a combination of Java and Scala as an alternative to other language choices such as Ruby:

Many programmers love Ruby, they just can’t get enough of it. Its probably one of the most aggressively evangelized languages since Java first came on the scene. They generally cite features that include a flexible and extensible syntax, closures, and how concise and expressive the code can be.

For example, you can create a Map (Ruby calls them “hashes”, even though a hashtable is only one way a map can be implemented) with a simple syntax like:

numberMap = {"one" => 1, "two" => 2, "three" => 3}

The Java equivalent of this is far more verbose:

Map<String, Integer> numberMap = new HashMap<String, Integer>(); numberMap.put("one", 1); numberMap.put("two", 2); numberMap.put("three", 3);

So where does Scala come in? Lets look at the map example in Scala:

var numberMap = Map("one" -> 1, "two" -> 2, "three" -> 3)

You will notice that it looks very similar to the equivalent code in Ruby, but there are some important differences. In particular, just like Java, the Scala compiler knows that numberMap uses Strings as keys, and Integers as values. Unlike Java, you didn’t have to tell it this, it just figured it out for itself! This is called “type inference”.

This means that if I try adding a new key-value pair to numberMap, but using an Integer for the key, and a String for the value, Scala will complain as soon as you try to compile it (or your IDE will warn you immediately). With Ruby, it is only likely to cause a problem when you run your software and try to retrieve this key and value from the Map and get an Integer and a String, rather than the String and the Integer that you were expecting.

It is hard to overemphasize what a timesaver compile-time type checking can be, it eliminates a whole class of bugs that would otherwise occur at runtime, and Scala brings you the benefits of this, without the verbosity.

To further show the reduction of code in a less trival example, Ted Neward explores the differences between a class developed in Java, C#, Visual Basic, Ruby and Scala in his blog posting Scala pt 2: Brevity.

Continuing on, Ian notes that:

Scala also has a bunch of other nice Ruby features, which Java lacks, including closures, and a very malleable syntax that makes it well suited for “domain specific languages”. It has all of these features, and combines them with the benefits of static typing.

This opinion is shared by David MacIver in the blog posting No, seriously, why Scala? where he points to object orientated programming, module orientated programming, static typing, functional programming, and implicits as features that he likes in the language.  He does add that

Scala's far from perfect. It has some syntactic weirdnesses, a few issues carried over from Java, a moderately buggy compiler and a host of little features and edge cases that are really hard to keep in your head. However, I find that these issues don't actually do more than annoy you from time to time. The core language is powerful and very useful for just sitting down and writing good code in.

Providing a balance outlook, David follows up with the blog post  Why not Scala where he expands upon some of the edge cases.  Summing up, David had the following comment:

All in all, I find these add up to just a bunch of annoyances. It's still my preferred language for the JVM, but depending on how you wait your priorities they might be more significant for you.

Showing that Scala as a language is maturing, the book Programming in Scala will soon be available.  For those that cannot wait, there is a pre-print version available as PDF from the Artima web site.

To make your own decision, and to learn more about Scala check out their website.

Rate this Article