Study note of the first three chapters of the book: Programming In Scala, 4th Edition.

Chapter 1: A Scalable Language

Scala stands for “Scala Language”. It grows on you because it allows defining new types and control structures (a code block as a parameter).

It is a pure OO language that all values are objects and it allows you to combine data and operations behand an interface.

It is functional. Functions are first-class values. Code block c can be wrapped in a function and be passed as a parameter (by-name parameter). Scala encourages the use of immutable data structure and functions without side-effects for the sake of referential transparency.

Why scala?

  • It is fully interpoerable with Java. It uses and enhances Java types.
  • It is concise by using simple syntax and supporting type inference. It supports mixed-in traits and comes with many powerful libraries.
  • It is high-level with a declarative programming style.
  • It is statically typed with good type inference and a rich type system (generics, intersections, and abstract types). The type can be used for verification, refactoring and coumentation.

Chapter 2: First Steps

The SScala type Int, Boolean, Float are compiled to corresponding Java primitive types.

The type of the value returned from a method is called a result type. It is better to specify the result type. The result type is mandatory in recursive function.

There are no ++ and -- operators in Scala.

In for (x <- xs), the x is a val value.

Chapter 3: Next Steps

You can instantiate an object using new. You parameterize/config an instance with values and types.

If a method takes only one parameter, you can call it using obj method parameter, without a dot and parentheses. Scala doesn’t have operators in the traditional sense. The so-called operators are actually methods.

When you apply parentheses surrounding one or more values to a varialbe, Scala transforms the code to call the apply method of the variable. So array(i) is actually array.apply(i). In assignment, array(i) = v is array.update(i, v). A betteer way to create an array is using the Array companion object such as Array(1, 2, 3) that is Array.apply(1, 2, 3).

An Array object has a fixed length of values that are of the same type. An array is mutable. List is an immutable object that has a fixed number of values that share the same type. ::: concatenate tow lists. The cons operator :: prepends an element to the front of a list. Nil or List() is an empty list. List favors prepending :: or :::, not appending :+, becausse appedning is slow.

A method name ending in a colon is right associated – it is a method of the right operatnad.

Tuples are immutable and can contain different types of elements. It is one-based, following the tradition of Haskell and ML. The type of a tuple depends on the number of elements and types of those elements.

Set and Map have both immutable implements (the default) ande mutable implements. To use the mutable type, import scala.collection.mutable.type. The -> is a method that returns a two element tuple.

It is good to prefer vals, immutable objects and methods without side effects.