Tuesday, April 9, 2013

Becoming Acquainted with Scala

There are many touted benefits of the Scala programming language, especially for Java developers. Among others, Scala's advertised strengths and advantages include the following:

  • Runs on the Java Virtual Machine (JVM)
    • Able to run on numerous hardware and operating systems platforms
    • Access to rich set of libraries and functionality in the JDK
    • Access to the broad Java ecosystem and its wealth of open source libraries and frameworks
    • Access to JVM features such as concurrency support and optimization
  • Supporting Functional and Object-oriented Paradigms
  • Statically Typed
  • Concise Syntax
  • Documentation
  • Maturity

The advantage Scala offers by running on the JVM are the same as those for the other numerous languages that run on the JVM such as Groovy, Jython, JRuby, Clojure, and Kotlin. However, the entire set of Scala offerings distinguishes it from the other languages.

One trait of Scala that distinguishes it from some of the other JVM-based languages is its being statically typed and static compilation. Groovy 2.0 introduces static compilation for Groovy, but several of the other JVM-based languages do not embrace static typing and static compilation because the language they emulate (such as Ruby or Python) are not static. Many developers do prefer dynamic typing, but for those of us who enjoy the advantages of static compilation and are comfortable with it from years of C, C++, and Java development, this is a welcome attribute of Scala. Static typing not only allows the compiler to do more work for me, but it makes it easier to develop better tools for the language.

Now that Groovy offers static typing and compilation, a more important differentiating factor when comparing Groovy and Scala is Scala's offering of functional programming paradigms. There are purported advantages of taking a more functional approach to software development, especially in terms of concurrency. Scala provides an opportunity for Java developers to experiment even more with the functional programming paradigm while keeping the ability to use object-oriented principles and enjoy the advantages of that paradigm. Clojure is an example of a language that more fully moves into the functional paradigm, leaving object-oriented approach behind.

One of the strengths of Scala that appeals to me most is the thorough documentation built around this language. The documentation tends to be clear and current. For example, the Getting Started with Scala document clearly and succinctly explains how to configure Windows or Linux/Unix for a Scala environment, how to run the Scala interpreter, how to script Scala, how to compile Scala, and how to execute Scala code. All of this is covered well in the equivalent of 2 printed pages.

Obtaining and configuring Scala is easy, especially if you have a version of the Java SDK already installed (Java SE 1.6 or later). Scala can be downloaded from the Scala Distribution page. There are multiple formats available for download and I downloaded the Scala 2.10.1 ZIP file for Windows. "Installation" was as simple as unzipping the contents of the ZIP file into my C:\scala-2.10.1 directory. I created a system-level environment variable SCALA_HOME that points to this directory and then added %SCALA_HOME%\bin to my system's Path environment variable. The reason I defined a SCALA_HOME environment variable to use in my Path instead of directly including the Scala installation directly in the Path is so that I can easily change the version of Scala in the future by simply changing the SCALA_HOME setting (this is also a way to switch between multiple versions of Scala installed on the same machine if necessary). Note that on Linux/Unix systems, ${SCALA_HOME} would be used instead of %SCALA_HOME%.

With the downloaded Scala archive file extracted into a directory on my system whose "bin" subdirectory is on my path, I can now open a terminal to see if Scala is configured properly. This is shown in the next screen snapshot where I use the command scala -version to see the version of Scala that has been installed and configured.

Another thing I like to do when first using a newly installed product is display its usage or help information. The next screen snapshot indicates doing this with Scala (scala -help).

The help information for the Scala launcher (scala) references support for all options supported by the Scala compiler (scalac). The next screen snapshot shows running that command to see those numerous options that apply to both scalac and scala.

Typing in "scala" at the command line without an option opens the Scala interpreter. This is shown in the next screen snapshot.

Access to the Scala interpreter allows me to start easily playing with Scala. I'll use the interpreter now to look at a few basics of Scala. The next screen snapshot shows how an arbitrary expression can be typed into the Scala interpreter and, if its valid syntax, will be processed and automatically assigned to a variable called "res0". In this case, the sum of adding 5 to 6 is set to "res0." The "res0" variable can then be used in the next evaluation, adding two to it. That sum, 13, is then automatically assigned to "res1". When I attempt to reassign the automatically generated "res0" to the value of 4, I see the error message: "error: reassignment to val."

The error just shown demonstrates one of the things I like about Scala; it allows differentiation of variables and constants (often referred to as mutable and immutable values). The val keyword is implicit in the implicitly created values. However, a Scala developer who declares his or her own variables and constants can explicitly use the keyword "val" to specify a constant and explicitly use "var" to specify a variable. This is demonstrated in the next screen snapshot.

To complete my "introduction to Scala" post, it is requisite that I include a "Hello, World" example in Scala. This example is actually provided in the already mentioned excellent Getting Started with Scala, but I include it here for convenience and to show how to compile and execute Scala source code.

object HelloWorld
   def main(args: Array[String])
      println("Hello, world!")

There are a few observations that can be made from looking at this simple code: Scala allows use of a new keyword "object" to specify a Scala object (singleton instance) and declares the name of the argument BEFORE the type of the argument (same applies to local variable/constant declarations). The code sample also shows that, like Groovy, Scala does not require java.lang.System.out to be explicitly statically imported or System.out.println to be spelled out each time used, uses the keyword "def" to define the main method, and does not require semicolons as long as each statement has its own line.

The next screen snapshot shows the contents of my HelloWorld.scala source file, shows running the Scala application without an explicit compilation step (it is compiled implicitly), shows compiling that source with scalac HelloWorld.scala, shows the two generated .class files that result from the compilation process, and shows running the simple Scala application with scala HelloWorld again.

Scala's perceived complexity and academic pedigree have scared some away from looking at Scala. Although Scala does appear to offer some extremely powerful and correspondingly complex features, the Scala language has many features that are not complex and are highly approachable. This post has demonstrated how easy it is to begin using Scala.

No comments: