A Tour of Scala


Type system

A typed programming language is a lanuage with the following syntactic components:

  1. Type description

  2. Data description

  3. Programming constructs


Types are descriptions in the source code about the data that are constructed in runtime.

Strongly typed language

A strongly typed language is one that requires the type information of all runtime data to be expressed in the source code.

Python is not strongly typed

def f(x, y):
    return str(x) + " and " + str(y)

We have no idea about the type information of x and y during runtime.

Clojure is not strongly typed

(defn first-and-last 
    (println "first is" (first seq) 
             ", and last is" (last seq)))

What type of sequence is seq? We don’t know from source code.

We have to wait until the code is executed at runtime.

Pure Object Oriented Programming (OOP)

Classes are types

  • All types are classes.

  • All data are objects.

All data must be instances of some class.

Strongly typed

The class of all data must be either be expressed explicitly or deduced from the source code.


  • An object must be an instance of some class.

  • An object can have zero or more members.

  • An object can have zero or more methods.

All computations are method invocations.

Java is not pure

Not all types are classes

  • Primitive types are not classes:

    float x = 3.1415f;
    x.getClass(); // Breaks
  • Arrays are not classes:

    int[] x = new int[]{1,2,3};

Not all computations are method invocations

  • Static methods do not belong to any object.

  • Arithmetic operators are not methods.

    1.+(2); // Break

About Scala

Scale is pure

  • All data are objects, belonging to some class.

  • All computation occurs in some method invocation.

Fixes Java

  • Primitive types are classes.

  • No static methods

  • More expressive type system

  • Functional programming with:

    • lambda functions

    • immutable data

    • closure

    • function as data

Symbol binding

Distinction between variable and value symbols

  • Variable symbols: their bindings can be updated.

  • Value symbols: they can be bound to data only once, and cannot changed.


var age: Int = 44

age = age + 1


val name = "Albert Einstein"

name = "John Doe"
cmd4.sc:4: reassignment to val
val res4_2 = name = "John Doe"
                  ^Compilation Failed
Compilation Failed

Scala Values

Everything is object

res4: Class[Float] = float
res5: Class[Double] = double
res6: String = "3.1415"

Everything is method invocation

We will explore this in far greater depth (time permits).

1 + 2
res7: Int = 3
res8: Int = 3

Scala Classes

A simple class

class Car {
    private var milesDriven: Int = 0
    def miles() = milesDriven
    def drive(distance: Int) {
        milesDriven += distance
defined class Car
val mycar = new Car()
mycar: Car = ammonite.$sess.cmd9$Helper$Car@21f068e1
res10_3: Int = 20

Classes are functions

Think of classes are functions that can take parameters and return objects. Thus, a class in Scala acts like a constructor.

class UsedCar(year: Int, miles: Int) {
    val yearOfCar = year
    var milesDriven = miles
    println(s"A used car from {yearOfCar} with {milesDriven}")
    def drive(distance: Int) {
        milesDriven += distance
defined class UsedCar
val mycar = new UsedCar(1975, 10000)
A used car from {yearOfCar} with {milesDriven}
mycar: UsedCar = ammonite.$sess.cmd11$Helper$UsedCar@51d4b6a3
res12_1: Int = 10000
res13_1: Int = 11000

Primary constructor

We can declare the members of a class as its parameters.

class UsedCar(val year: Int, var miles: Int) {
    def drive(distance: Int) {
        miles += distance
defined class UsedCar
val mycar = new UsedCar(2000, 10000)
mycar: UsedCar = ammonite.$sess.cmd14$Helper$UsedCar@fa0948
res15_1: Int = 10000
res16_1: Int = 12000