*DISCLAIMER: THIS ARTICLE IS NOT A SCALA TUTORIAL, IT'S A BRIEF INTRODUCTION TO SCALA LANGUAGE AND THE REASONS WHY YOU SHOULD BE INTERESTED IN LEARNING IT.
In my last blog post I wrote about alternative languages to use in the JVM and the reasons why it's good to have more than one in your toolbox. Continuing with this idea we are going to analyze in more detail one of the most solid alternative JVM languages we have at the moment: Scala. I had to use Scala for a big, real time social media data analysis system I worked on last year and while at first it was hard, I ended up having a blast using it.
According to several sources [1, 2] Scala is the second most popular JVM language after Java itself, so that makes it an obvious candidate if you are looking for an alternative to Java but you want to stay using the JVM. With a mature toolset, a strong community and technical support, it’s widely used and well understood.
There are a few well known open-source projects using Scala: Apache Spark cluster computing solution and Apache Kafka a publisher subscriber message queue, to name two of them. Twitter jumped from Ruby to Scala in 2009 for performance reasons and have since contributed a lot to the language development and adoption. Scala has been widely adopted across multiple areas by companies such as Netflix, Twitter, Facebook and Amazon and is commonly used for entire applications, most notably Big Data. It’s safe to say the Big Data feature set is the "Killer Domain" of Scala.
Scala is short for “Scalable Language” and it is a general purpose programming language. It's designed to embrace multiple programming paradigms, mainly merging object oriented and functional programming within a strong static type system. Scala was also designed to tackle some of the most important shortcomings and criticisms of Java.
Scala is more expressive, concise, and safer than Java. It has a strong emphasis on immutability, lazy evaluation, solid functional programming concepts and improved object oriented constructs, including a safer static type system.
Scala was also designed to have an easy start path to using it effectively if you are coming from Java, C# or a similar language, and so it can be used as a "better Java than Java" with a slightly different syntax, but Scala also incorporates more advanced functional and type constructs than Java, making Scala a completely different language capable of expressing problems in different ways.
Java 8 introduced some important functional programming capabilities, but how do these compare the ones in Scala? I won’t make a real comparison here because that would require a separate post, but I will say that Java 8 functional programming capabilities are a valuable addition to the language but are not as powerful as Scala’s capabilities. Scala also has an expensive and flexible syntax with a strong domain of specific language building capabilities.
Scala not only supports but extends all of the object-oriented features available in Java, and its greatest strength resides in its functional programming capabilities. This allows Scala programs to be written in an almost completely functional style, an almost completely object-oriented style, or the idiomatic Scala way that is a mix of both paradigms. Some of the advanced functional programming capabilities included in Scala are:
One of the biggest problems in concurrency programming is mutable state. Mutable state requires a lot of effort to ensure correctness, it’s very prone to error and it produces bugs difficult to find and fix. It also makes code that is hard to understand, analyze, predict and reason about. Functional programming solves this problem by not having mutable state so no function can have side effects.Though Scala promotes this way of thinking, it doesn't force it. You can still have mutable state in Scala, it's just strongly discouraged. The Scala mindset is to use mutable state only if you have a very good reason to do it.
Scala:
Java:
In Scala, types are declared in a postfix way but are only needed in places where the compiler can't infer them. Method definitions start with the reserved word def, code block braces are not required for single statement code statements, and the return operator is optional because methods return the value of their last statement. Casts in Scala are calls to asInstanceOf method, and Scala constants are declared with val, with variables been declared with var.
Scala:
Java:
As you can see, there is a lot less syntactic boilerplate in Scala than there is in Java. That concept of syntactic simplicity and flexibility is extended all over the language making it possible to easily define Domain Specific Languages in Scala without the need of language extensions. Another important difference between Java and Scale is that Scala has a unified type system without the Java distinction between primitive types and reference types.
Scala is not perfect, no programming language is. Some outstanding issues include:
So you’re interested in learning about this OO/Functional hybrid language?
My first advice is do it a step at a time, like Toby Weston suggested in this blog. You can start using Scala from an OO mindset and introduce the Functional aspects slowly until you and your team feel comfortable.