Programming Scala: Tackle Multi-Core Complexity on the Java Virtual Machine
Buy it now
One Minute Bottom Line
This book has practically no caveats. However, perhaps the subtitle "Tackle
Multicore Complexity on the Java Virtual Machine" is a little bit
misleading. The chapter on concurrent programming does a nice job
introducing actors as a lightweight mechanism for concurrency, but this
is not a book that specifically targets Scala concurrency. Rather, it
introduces it as another interesting Scala aspect through the actors model.
When I laid my hands on this book I was delighted to see that it's part of "The Pragmatic Programmers" series. As a developer I have a high respect for books in this series because they often go straight to the point and favor a hands-on approach. In this sense "Programming Scala" was no exception. Written in an easy-to-follow conversational style it makes the perfect introduction to Scala for busy Java developers (and yes, the author assumes you're a proficient Java developer and relies on what you already know to show you the "Scala way" of programming).
Let's take a quick look at each chapter:
Getting Up to Speed with Scala
This section tells you everything you need to know to get Scala from the web, install it on Windows or Unix systems, try it on the command line or as a shell script and to use it from your favorite Java IDE.
Classes in Scala
In this chapter you're introduced to Scala departing from Java code. It shows how initially you don't necessarily have to quit your Java-like imperative coding style but the power of a more functional and OO approach is evident. Several examples help you understand that there are no primitive types in Scala, the power of tuples and multiple assignments, and the flexibility of operator overloading (operators are in fact methods).
In this chapter you're introduced to Scala's sensible static typing and type inference (including Java Generics collections). Three special types are especially helpful here (Any, Nothing and Option). You'll see that Scala not only infers the types of your variables but also tries to infer the return types of your methods making it easier for you to write concise code. You'll also see how to pass variable arguments to methods (Varargs) and how to bypass covariance and contravariance restrictions in collections.
Function Values and Closures
Here you're introduced to Function Values which are higher order functions in Scala that allow you to pass functions to functions as parameters, return them from functions and even nest them as parameters. You'll also learn how to use closures, a special form of function values that close over or bind to variables defined in another scope or context. Using these constructs as building blocks in your application you can quickly come up with concise, reusable code.
Traits and Type Conversions
In this chapter you'll be intorduced to the power of traits in Scala. Traits could be seen as interfaces with a partial implementation (a behavior) providing a middle ground between single and multiple inheritance because you can include them in other classes to enhance them with a set of features. Traits are often used to decorate objects with capabilities and you'll see such examples in this section. You're also introduced to implicit type conversion in Scala which can help you extend the language to create your own vocabulary or syntax specific to your application or your own DSL.
This chapter focuses on the Scala specific collections List, Set and Map (note that you can still use the JDK collections). By default these collections are immutable but you can also point Scala to use mutable versions.
Pattern Matching and Regular Expressions
This is the 2nd most widely used feature of Scala after function values and closures. In this section you'll see how to use Scala's mechanism for pattern matching, the case classes and extractors (allowing for matching of more complicated patterns) and how to create and use regular expressions.
This is a great introduction to Scala's actors model (reminds me of Erlang's model) used to allow threads to communicate using an event based system to send immutable objects as messages. Unlike concurrency in the Java language you won't have to deal with synchronization and exceptions, as long as you pass around immutable objects you won't have to worry about contention.
Intermixing with Java
After reading this chapter you'll realize how easy it is to use Java classes in Scala and vice versa. Scala complies to bytecode just like Java so as long as you include the Scala library you can bring the power of Scala to your Java application (the chapter is targeted at Java developers that want to take full advantage of Scala in their Java applications). One key strength of Scala is that it fully supports the Java semantics and it further extends it with functional style.
Unit Testing with Scala
This is pretty straight forward in Scala, you can either go with JUnit, TestNG or ScalaTest (the later is written in scala itself). The chapter introduces your to JUnit and ScalaTest.
Unlike Java, Scala does not force you to catch exceptions you don't care about (not even checked exceptions), they'll automatically propagate up the chain. Interestingly enough you'll see how to use pattern matching to handle the exceptions you care about. In Scala, both catching and declaring checked exceptions is optional. Overall the chapter shows an exception handling strategy in Scala that is concise and elegant.
Finally you put all you learned to the test by building a sample application in Scala (an application to find the net worth of investments in the stock market). Among other goodies you'll learn about the power of pattern matching along with closures, how to deal with the concurrency API, Scala's support for XML processing and how to build a Swing application.
If you're looking fo more resources on the book, you should check the book's official page where you'll find the source code, errata and a discussions section.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)