# Notes of Scala course on Coursera -- Week 3

Here are my notes for the week 3’s lecture of Functional Programming Principles in Scala on Coursera.

## 1. Class Hierarchies

### 1.1 Abstract Classes

Consider the task of writing a class for sets of integers with the following operations.

IntSet is an abstract class.

Abstract classes can contain members which are missing an implementation (in our case, incl and contains ).

Consequently, no instances of an abstract class can be created with the operator new.

### 1.2 Class Extensions

Let’s consider implementing sets as binary trees.

There are two types of possible trees: a tree for the empty set, and a tree consisting of an integer and two sub-trees.

Here are their implementations:

Empty and NonEmpty both extend the class IntSet .

This implies that the types Empty and NonEmpty conform to the type IntSet

• an object of type Empty or NonEmpty can be used wherever an object of type IntSet is required.

### 1.3 Base Classes and Subclasses

IntSet is called the superclass of Empty and NonEmpty .

Empty and NonEmpty are subclasses of IntSet .

In Scala, any user-defined class extends another class.

If no superclass is given, the standard class Object in the Java package java.lang is assumed.

The direct or indirect superclasses of a class C are called base classes of C .

So, the base classes of NonEmpty are IntSet and Object.

### 1.4 Implementation and Overriding

The definitions of contains and incl in the classes Empty and NonEmpty implement the abstract functions in the base trait IntSet .

It is also possible to redefine an existing, non-abstract definition in a subclass by using override

### 1.5 Object Definitions

This defines a singleton object named Empty .

No other Empty instances can be (or need to be) created.

Singleton objects are values, so Empty evaluates to itself.

Run Scala Application Program in command line:

In command line:

#### Exercise

Write a method union for forming the union of two sets. You should implement the following abstract class.

### 1.6 Dynamic Binding

Object-oriented languages (including Scala) implement dynamic method dispatch.

This means that the code invoked by a method call depends on the runtime type of the object that contains the method.

#### Example

Another evaluation using NonEmpty :

## 2. How Classes are Organized

### 2.1 Packages

Classes and objects are organized in packages.

To place a class or object inside a package, use a package clause at the top of your source file.

This would place Hello in the package progfun.examples .

You can then refer to Hello by its fully qualified name progfun.examples.Hello . For instance, to run the Hello program:

### 2.2 Import

Say we have a class Rational in package week3 .

You can use the class using its fully qualified name:

Alternatively, you can use an import :

Imports come in several forms:

The first two forms are called named imports.

The last form is called a wildcard import.

You can import from either a package or an object.

#### Automatic Imports

Some entities are automatically imported in any Scala program.

These are:

• All members of package scala
• All members of package java.lang
• All members of the singleton object scala.Predef

### 2.3 Traits

In Java, as well as in Scala, a class can only have one superclass.

But what if a class has several natural supertypes to which it conforms or from which it wants to inherit code?

Here, you could use trait s.

A trait is declared like an abstract class, just with trait instead of abstract class

Classes, objects and traits can inherit from at most one class but arbitrary many traits.

Example:

Traits resemble interfaces in Java, but are more powerful because they can contains fields and concrete methods.

On the other hand, traits cannot have (value) parameters, only classes can.

### 2.5 The Nothing Type

Nothing is at the bottom of Scala’s type hierarchy. It is a subtype of every other type.

There is no value of type Nothing .

Why is that useful?

• To signal abnormal termination
• As an element type of empty collections (see next session)

### 2.6 Exceptions

Scala’s exception handling is similar to Java’s.

The expression

aborts evaluation with the exception Exc .
The type of this expression is Nothing .

### 2.7 The Null Type

Every reference class type also has null as a value.

The type of null is Null .

Null is a subtype of every class that inherits from Object ; it is incompatible with subtypes of AnyVal .

## 3. Polymorphism

### 3.1 Cons-Lists

A fundamental data structure in many functional languages is the immutable linked list.

It is constructed form two building blocks:

• Nil, the empty list
• Cons, a cell containing an element and the remainder of the list.

### 3.2 Cons-Lists in Scala

Here’s an outline of a class hierarchy that represents lists of integers in this fashion:

A list is either

• an empty list new Nil, or
• a list new Cons(x, xs) consisting of a head element x and a list xs .

#### Value Parameters

Note the abbreviation (val head: Int, val tail: IntList) in the definition of Cons .

This defines at the same time parameters and fields of a class.

It is equivalent to:

where _head and _tail are otherwise unused names.

#### Type Parameters

It seems too narrow to define only lists with Int elements.

We’d need another class hierarchy for Double list, and so on, one for each possible element type.

We can generalize the definition using a typing parameter:

Type parameters are written in square brackets, e.g. [T].

#### Generic Functions

Like classes, functions can have type parameters.

For instance, here is a function that creates a list consisting of a single element.

We can then write:

In fact, the Scala compiler can usually deduce the correct type parameters from the value arguments of a function call.

#### Types and Evaluation

Type parameters do not affect evaluation in Scala.

We can assume that all type parameters and type arguments are removed before evaluating the program.

This is also called type erasure.

Languages that use type erasure include Java, Scala, Haskell, ML, OCaml.

Some other languages keeps the type parameters around at run time, these include C++, C#, F#.

#### Polymorphism

Polymorphism means that a function type comes “in many forms”.

In programming it means that

• the function can be applied to arguments of many types, or
• the type can have instances of many types

We have seen two principal forms of polymorphism:

• subtyping: instances of a subclass can be passed to a base class
• generics: instances of a function or class are created by type parameterization.

#### Exercise

Wirte a function nth that takes an integer n and a list and selects the n’th element of the list.

Elements are numbered from 0.

If index is outside the range from 0 up the length of the list minus one, a IndexOutOfBoundsException should be thrown.