Skip to main content
main-content
Top

About this book

Scala is a new programming language developed by Martin Odersky and his team at the EPFL. The name Scala is derived from Sca(lable) La(nguage). Scala is a multi-paradigm language, incorporating object oriented approaches with functional programming. Although some familiarity with standard computing concepts is assumed (such as the idea of compiling a program and executing this compiled form, etc.) and with basic procedural language concepts (such as variables and allocation of values to variables) the early chapters of the book do not assume any familiarity with object orientation nor functional programming. These chapters also step through other concepts with which the reader may not be familiar (such as list processing).
From this background, John Hunt provides a practical introduction to object and functional technology using Scala, one of the newest and most interesting programming languages available. A variety of concepts are introduced through practical experience taking the reader beyond the level of the language syntax to the philosophy and practice of object-oriented development and functional programming.
Students, and those actively involved in the software industry will find this comprehensive introduction to Scala and to object orientation and functional programming, invaluable.

Table of Contents

1. Introduction

Abstract
This book is intended as an introduction to Scala for computer science students or those actively involved in the software industry. It assumes some familiarity with standard computing concepts such as the idea of compiling a program and executing this compiled form, etc., and with the basics of procedural language concepts such as variables and allocation of values to variables etc. However, the early chapters of the book do not assume any familiarity with object orientation nor functional programming. They also step through other concepts with which the reader may not be familiar (such as list processing etc.). From this background, it provides a practical introduction to object and functional technology using Scala, one of the newest and most interesting programming languages available.
John Hunt

2. Elements of Object Orientation

Abstract
This chapter introduces the core concepts in Object Orientation. It concisely defines the terminology used and attempts to clarify issues associated with hierarchies. It also discusses some of the perceived strengths and weaknesses of the object oriented approach. It then offers some guidance on the approach to take in learning about objects.
John Hunt

3. Why Object Orientation?

Abstract
The pervious chapter introduced the basic concepts behind object orientation, the terminology and explored some of the motivation. This chapter looks at how object orientation addresses some of the issues that have been raised with procedural languages. To do this it looks at how a small extract of a program might be written in a language such as C, considers the problems faced by the C developer and then looks at how the same functionality might be achieved in an object oriented language such as Scala, Java or C#. Again do not worry too much about the syntax you will be presented with, it will be Scala but it should not detract from the legibility of the examples.
John Hunt

4. Constructing an Object Oriented System

Abstract
This chapter takes you through the design of a simple object oriented system without considering implementation issues or the details of any particular language. Instead, this chapter illustrates how to use object orientation concepts to construct a software system. We first describe the application and then consider where to start looking for objects, what the objects should do and how they should do it. We conclude by discussing issues such as class inheritance, and answer questions such as “where is the structure of the program?”.
John Hunt

5. Functional Programming

Abstract
Previous chapters have focussed on the object-oriented side of the Scala language. However, Scala is a hybrid Object Oriented (OO) and Functional Programming (FP) language. In this chapter we will now look at Functional Programming and its advantages and disadvantages.
John Hunt

6. Scala Background

Abstract
I first encountered the Scala language in 2010. I was working on a graduate training programme for an international banking organisation when I was asked to give an hour talk to the graduates on Scala. At that point I had heard the name mentioned but had no idea what it was. I therefore did some reading, installed the IDE being used and tried out some examples—and was hooked.
John Hunt

7. A Little Scala

Abstract
In the last chapter, you learned a little about the history of Scala and the Scala development environment. In this chapter, you encounter a little of the Scala language, what happens when you compile and run a Scala program, the Scala Runtime (Virtual Machine) and the Scala IDE.
John Hunt

8. Scala Building Blocks

Abstract
This chapter presents an introduction to the Scala programming language. As such, it is not intended to be a comprehensive guide. It introduces the basic elements of the Scala language, including Apps, discusses the concept of classes and instances and how they are defined, presents methods and method definitions, and considers constructors and their role.
John Hunt

9. Scala Classes

Abstract
This chapter considers the constructs in Scala used to define classes.
John Hunt

10. Scala Methods

Abstract
This chapter presents how methods and associated behaviour is defined in Scala.
John Hunt

11. Building a Class

Abstract
This chapter we will work through the creation of a simple class to represent a Company.
John Hunt

12. Packages & Encapsulation

Abstract
This chapter discusses the encapsulation and packaging features of Scala. The concept of packages is discussed, along with some concrete examples. It then illustrates how the encapsulation facilities can allow quite fine-grained control over the visibility of the elements of your programs.
John Hunt

13. Classes, Inheritance and Abstraction

Abstract
Inheritance is one of the most powerful features of Object-Orientation. It is the difference between an encapsulated language that provides an object-based model and an object-oriented language. Inheritance is also one of the main tools supporting reuse in an object-oriented language (although in Scala’s case Traits are also a major tool for reuse). You will use inheritance all the time without even realising it, indeed you have already been doing so every time you have benefited from the default implementation of toString (which is inherited through the class AnyRef by your classes if you do not explicitly extend any specific class).
John Hunt

14. Objects and Instances

Abstract
This chapter will discuss the difference between objects in Scala and Instance of a class. This is important as many other object-oriented languages use these terms interchangeably. However, in Scala they are significantly different concepts, defined with different language constructs and used in different ways.
John Hunt

15. Value Classes

Abstract
This chapter introduces another type of class in Scala; Value Classes. A value class is a type where the actual value being represented by the class is held directly by a variable, rather than needing to that value via a reference (an address in memory). Examples of value types include Booelan, Int and Double which can have the values true, false, 32, 45.7 etc. Such values can be held directly by a variable, rather than accessed via a reference. This can be more efficient for simple types like Int.
John Hunt

16. Scala Constructs

Abstract
This chapter presents more of the Scala language. It considers the representation and use of numbers, strings and characters. It also discusses assignments, literals and variables. Finally, it considers messages, message types and their precedence.
John Hunt

17. Control and Iteration

Abstract
This chapter introduces control and iteration in Scala. In Scala, as in many other languages, the mainstay of the control and iteration processes are the if and switch statements and the for and while loops.
John Hunt

18. Traits

Abstract
In the previous chapters there have already been several references to these things called Traits. For example, we have encountered the App trait numerous times. However we have avoided the question “What are traits?”.
John Hunt

19. Further Traits

Abstract
This chapter looks at some of the more advanced features associated with the use of traits. The chapter looks at the way in which Traits can be dynamically wrapped around an instance providing a form of Aspect Oriented Programming (AOP) known as stackable traits. We then look at the role Traits can play in developing reusable behaviour that simplifies the development of new types. Universal traits used with Value Types are then discussed. The chapter concludes by considering the way in which traits can be used to define a restricted set of values for a given type.
John Hunt

20. Arrays

Abstract
This chapter discusses how arrays are represented in Scala. Arrays are (logically) a continuous set of slots that can hold values or references to instances of a specific type.
John Hunt

21. Tuples

Abstract
In this chapter we introduce Scala tuples, which are a useful (and very simple construct) for grouping instances together.
John Hunt

22. Functional Programming in Scala

Abstract
This chapter examines the functional programming features of Scala and looks at how they can be used while the next chapter will look at concepts such as Currying and Partially Applied functions.
John Hunt

23. Higher Order Functions

Abstract
Functions in Scala are part of the type system. That is, they are part of the system of entities that comprise the types that variables and parameters can reference. Thus just as a parameter can be of type Int, or Boolean, or Person a parameter to a method or a function can be another functions. A function that takes a function as a parameter is referred to as a Higher-Order Function. This chapter discusses Higher-Order Functions and how they can be used and provides some examples taken from the collection classes in Scala.
John Hunt

24. Partially Applied Functions and Currying

Abstract
This chapter looks at two ways in which functions (and in fact methods) in Scala can comprise components of reuse within a software system. These two approaches are partial application of functions and Currying. The two approaches represent variations on a theme. In both cases they allow a function with one or more parameters to be have one or more of those parameters bound to a specific value to create a new function with one or more fewer variables.
John Hunt

25. Scala Collections Framework

Abstract
The Collections framework is one of the main categories within the set of Scala libraries that you will work with. It provides types (Traits, Objects and Classes) that support various types of data structures (such as lists and maps) and ways to process elements within those structures. This chapter introduces the Scala Collections framework.
John Hunt

26. Immutable Lists and Maps

Abstract
This chapter focuses on the List and Map types from the scala.collection.immutable package.
John Hunt

27. Immutable and Mutable Collection Packages

Abstract
This chapter discusses the contents of the Scala collection framework packages for immutable and mutable collections.
John Hunt

28. Type Parameterization

Abstract
This chapter introduces type parameterization. We have already seen examples of type parameterization in the collection types where a List can be parameterized to hold only Strings, Person objects or integers etc. In this chapter we will look at how you can create your own parameterized types and the options available for controlling the types that can be used with those parameterizations.
John Hunt

29. Further Language Constructs

Abstract
This chapter introduces a number of new concepts. The first is the use of implicit. Implicit facilities are a range of language facilities that provide implicit conversions between one type and another (typically in order to access some functionality or to provide compatibility between types). The chapter then introduces Scala annotations (a form of meta data for types), Enumerations and lazy evaluation.
John Hunt

30. Exception Handling

Abstract
This chapter considers exception handling and how it is implemented in Scala. You are introduced to the object model of exception handling, to the throwing and catching of exceptions, and how to define new exceptions and exception-specific constructs.
John Hunt

31. Scala and JDBC Database Access

Abstract
In this chapter we will primarily look at how Scala can use a database connectivity API referred to as JDBC. Strictly speaking this is available to Scala when run on a JVM due to its relationship to the byte code environment of the JVM. Officially JDBC is not an acronym, however to all intents and purposes it stands for Java DataBase Connectivity. This is the mechanism by which relational databases are accessed in Java. As Scala runs in the same environment as Java Scala programs also have access to JDBC. At the time of writing it is the simplest and most stable approach to database access in Scala; however it is not the only approach and we will look at some of the more Scalaesque approaches in the next chapter.
John Hunt

32. Scala Style Database Access

Abstract
The last chapter introduced how Scala can use JDBC to access a database. However, there a number of different Scala based approaches being developed to explore more Scala like ways of interacting with a database than JDBC. Although JDBC works very well it is far more Java like than Scala like and actually provides a very low level of abstraction (witness the many Object Relational Mapping, or ORM, tools within the Java world such as Hibernate).
John Hunt

33. Scala and MySQL Database Example

Abstract
In this chapter we will be writing some simple functions to run some queries against a simple database. The chapter uses MySQL. This is an open source database management system that can be, and is used, for commercial systems.
John Hunt

34. Testing

Abstract
This chapter considers the different types of tests that you might want to perform with the systems you develop in Scala. It also introduces Test Driven Development.
John Hunt

35. Scala Testing

Abstract
This chapter examines the various facilities available within Scala to perform a range of tests.
John Hunt

36. Introduction to Akka Actors

Abstract
This chapter introduces the concept of an Actor as an approach to the development of concurrent programs. In particular we focus on the Scala Akka implementation of the Actor model based on release 2.2.3 (the current release at the time of writing). Note that Scala 2.10 also has its own scala.actor implementation of the Actor model however the Akka approach is more sophisticated and is likely to migrate into Scala.
John Hunt

37. Further Akka Actors

Abstract
The previous chapter introduced the basic Akka Actor concepts and constructs. This chapter takes these concepts further looking at how a result can be returned by (obtained from) an Actor. It also looks at Actor hierarchies and Actor supervision.
John Hunt

38. Play Framework

Abstract
The Play framework is a lightweight, stateless, asynchronous, framework for building web applications and services. It is built on top of Scala and the Akka concurrency API and aims to provide predictable behaviour with minimal resource consumption (i.e. CPU, memory, threads) for highly-scalable applications. Play is open source and can be obtained from http://www.playframework.com.
John Hunt

39. RESTful Services

Abstract
This chapter looks at RESTful web services as implemented using the Play framework.
John Hunt

40. GUIs in Scala Swing

Abstract
This chapter describes how to create rich client graphical displays (desktop application) using the Scala Swing windowing and graphical types. Thus in this chapter we consider how windows, buttons, tables etc. are created, added to windows, positioned and organised in Scala.
John Hunt

41. User Input in Scala Swing

Abstract
The last chapter looked at various different types of user interface component available within Scala Swing. This chapter now looks at how user input, via those components, can be handled.
John Hunt

42. Scala Build Tools

Abstract
There are many ways in which a Scala application can be built. These include the REPL loop and automated compilation within an IDE such as the Eclipse based Scala IDE. However, neither of these is suitable for centrally building large applications as might be found within many commercial organisations. The most popular build environments for Scala are Maven and the SBT. In this chapter we will briefly examine both so that you have a flavour of both tools and the potential benefits and drawbacks of each.
John Hunt

43. Scala & Java Interoperability

Abstract
In this chapter we will look at the interoperation of Java and Scala. Both Java and Scala are JVM Byte Code Languages. That is, they both compile to the byte code language that is understood by the JVM. The Byte Code language of the JVM was originally designed to be the compiled form of Java and was what the Java Virtual Machine executed. However, things have evolved such that today the JVM is a virtual environment for executing Byte Code languages. In fact there are now several languages that can be compiled to JVM Byte Codes including Java, Groovy, Clojure, Jruby, Jython, JavaScript, Ada, Pascal as well as Scala.
John Hunt
Additional information