An Introduction to Scala

Better Software Magazine
Volume-Issue: 
2010-06
Summary:

Scala is a programming language that blends functional and object-oriented language features. Scala programs run on the Java Virtual Machine and can easily interact with Java code. Learn how Scala can yield concise, safe, and compatible code and how you can start learning Scala on your own.

Scala is a programming language designedto be concise, safe, and compatible. Programs written in Scala run on the Java Virtual Machine and can reuse existing Java libraries and code. Scala is already used in production for business-critical sys­tems; it powers several services at Twitter [1] including mes­sage queues, the streaming API, and people search function. Foursquare’s main and mobile websites are written in Scala [2]. In addition, Scala is used in projects at companies such as LinkedIn, Novell, Sony Pictures Imageworks, and Siemens. Martin Odersky, Scala’s creator, has a strong language design background: He wrote Sun’s Java 1.3 compiler and cocreated Generics for Java.

Scala refines the object-oriented features of Java and bor­rows functional programming techniques from languages in­cluding ML and Haskell. This yields code sizes that are typi­cally reduced by a factor of two to three when compared to equivalent Java code [3] and draws aesthetic comparisons to other dynamic languages [4] like Ruby. But Scala code is fast—with performance on par with Java [5] and faster than Ruby.

One of Scala’s key strengths is its excellent compatibility with Java. Scala’s compiler compiles Scala source files to Java class files (bytecode), and its runtime libraries are straight Java JARs. Scala code can call any existing Java code you have, so you can reuse all that Java code you’ve written. This means you can write code in Scala and run it anywhere you would run Java code—from desktop Swing applications to web ap­plications powered by servers such as Jetty or Glassfish.

Functional Programming
The functional programming style is an alternative to the imperative style often used in object-oriented languages. Functional programming languages model their computations in terms of side effect-free functions, like mathematical func­tions—the output of a function depends only upon its inputs. If you call a function with some input value, it will yield the same output no matter how many times you call the function. Compare this to an object-oriented language, where calling a method on an object may give you a different result on every invocation if an object’s state may change (for example, a GUI window object’s getPosition()method will return different values as the window is moved around the screen).

Functional languages are written in terms of immutable values; once a variable is assigned, it can never change. Think of final variables in Java—those are immutable values. Here’s a sample assignment in Scala: 

val theAnswer = 42

To declare an immutable value in Scala, you use the val keyword. Any attempt to reassign the value will result in a compiler error.

Furthermore, functions are first-class values just like num­bers and strings; they can be assigned to variables and passed as arguments to functions. Here is how to find the even num­bers between one and eight:

val numbers = List(1, 2, 3, 4, 5, 6, 7, 8)

numbers.filter { x => ( x % 2 ) == 0 }

// Result is List(2, 4, 6, 8)

This code creates a list containing the values one through eight and assigns it to the variable “numbers.” The “filter” function on List is invoked, which evaluates the list and re­turns a new list containing only the items for which the speci­fied function evaluates to true. In this case, we specify a func­tion (between the curly brackets) that takes a variable “x” and returns a Boolean indicating if x modulo 2 is zero (an­other way of asking “Is x even?”)

Functional programs can be simpler to understand than programs with mutable state, since a function’s result de­pends only upon its inputs. If you’ve tried to understand how a method on

File: 
AttachmentSize
An_Introduction_to_Scala.pdf663.62 KB

About the author

Daniel Wellman's picture
Daniel Wellman

Daniel Wellman is a technical lead at Cyrus Innovation, a leading agile consultancy based in New York, where he leads development projects and coaches teams on adopting agile software development practices. Daniel has more than ten years of experience building software systems and is an expert in agile methodologies, object-oriented design, and test-driven development. Contact Daniel at dan@danielwellman.com.

Upcoming Events