Scala 05 Oct 2008 02:48 pm
Introduction to Scala
For quite some time now a number of people whom I respect highly have been talking quite consitently about the Scala programming language. It has also been mentioned a number of times on “The Java Posse” pod cast. As a result my interest in Scala was sparked and I figured it is time I get my hands dirty and explore why there is so much buzz about the language.
My plan is to document what I learn about the language on my blog as a self-reference. Hopefully others will find my postings useful… or at the very least spark more interest.
What is scala?
Scala is a Sca(lable) la(nguage). Many sites about Scala talk about the language being scalable since it can lend itself to writing small scripts or to building large systems. To be honest I am not convinced I would use it for writing small scripts. I think there are better suited technologies available. When it comes to building larger systems I think it has a lot of promise … more so than Ruby. Ruby has some serious issues that makes writing large systems difficult. I am not going to discuss those issues here though.
An application written in Scala compiles into Java byte code and runs inside the JVM. As a result it offers interoperability with Java that few other languages offer.
Scala is both an object orientated, and functional language. Taken from the scala web site (http://www.scala-lang.org/)
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. Classes 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.
While Scala is statically typed, its type system is highly advanced and resolves many of the issues that people site as problems of type strong languages. I shall discuss a little more about this later.
Through the use of a unique combination of language mechanisms Scala is highly extensible. Its ability to add new language constructs makes it ideal for developing domain-specific languages.
Why scala?
For me, Scala’s main attraction is its interoperability with Java. From Scala you can call Java methods, access Java Fields, inherit from Java classes, and implement Java interfaces. Scala code can also be invoked from Java code. This level of interoperability opens up a huge world or open source and commercial libraries that are available to the Java community also available to the Scala community. It also means that existing Java systems can be extended with Scala with out much effort.
Like Ruby and Groovy, Scala allows programs to be written concisely and expressively. A common example for illustrating Scala’s conciseness is the declaration of a class.
Consider the following Java class:
class Person {
private final String firstName;
private final String lastName;
private final int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
If we were to rewrite this class in Scala, it could be written like this:
class Person(firstName: String, lastName: String, age: Int)
As I have mentioned already, Scala’s type system is highly advanced. The local type inference mechanism of Scala means redundant type information does not have to be explicitly annotated.
For example:
val y: Int = 1 + 3
val y = 1 + 3
These two statements are equivalent. In both cases y is an integer but on the second line the type of Int is inferred by its usage.
For me another good reason of why Scala is worth investigation is its testability. With its close interoperability with Java, well established test frameworks such as JUnit and TestNg can easily be used for unit testing. In addition there are a number of Scala specific testing frameworks such as ScalaTest, Rehersal, specs, and ScalaCheck.
In recent days I have started to use specs as my unit testing framework of choice and will discuss its use in a future blog about unit testing Scala.

