Study note of chapter 4 and 5 of the book: Programming In Scala, 4th Edition.

Chapter 4: Classes and Objects


A class is a blueprint for objects. Use var or val to define fields. Use def to define methods. Fields and methods are collectively called memebers. The default access level of members is public.

Use new ClassName to instantiate objects of the class that doesn’t have parameters. Class fields are also called as instance variables. Fields are object’s states and should be private.

Method parameters are val variables. The last value of method returs. For single expression method, the curly braces are optional. It is a best practice to provide the result type of public methods.

A side effect is defined as mutating state somewher external to the method or performing an I/O action. a method that is executed for side effects is known as a procedure. The result type of a procedure is Unit.

Semicolon Inference

The semicolon is used to mark the end of a statement. Scala can infer the semicolon. It is common to put infix operator at the end of a line. The rule of semicolon inference is that a line ending is treated as a semiclon except

  • the ending word is not a legal statement ending. For example, a period or an infix operator.
  • the next line begins with a word that cannot start a statement.
  • the line ends inside () or [] because they can not contain multiple statments.

Single Object

Use the keyword object Name {} to define a singleton object. If a singleton object shares the same name with a class defined in the same file, the singleton object is called the companion object of the class and the class is the companion class of the singleton object.

A class cannot have static methods. A singleton is an object that acting as a holder of static methods. It can also have, usually private, fields.

A singleton object can extend a super class and can mix in traits. A singleton object can not take parameters. It is implemented with a syntehic class whose name is the object name plus a dollar sign $.

A singleton object without a companion class is called a standalone object. A standalone object with a def main(args: Array[String]) = {...} can be used an entry point of an application.

Scala implicitly imports java.lang._ (a package), scala._ (a package), and Predef._ (a singleton object). The println is a method defined on Predef that calls Console.println.

Scala allows different names for a file and classes or single objects. A Scala source file that ends in a definition is not a script. Use scalac to compile Scala source files into classes. Use fsc for better performance and use fsc -shutdown to stop the server daemon.

You can also define an application entry by extending from the App trait and using args to access command-line arguments.

Chapter 5: Basic Types and Operations


Integeral types: Byte, Short, Int, Long, Char Numberic types: integral types, Float and Double

Boolean and numeric types are define in scala package. They are compiled to corresponding Java primitive types. String is defined in java.lang.

A literal is a constant value directly written in code. Hexadecimal begins with 0x or 0X. Default integral type except Char is Int. Use l or L postfix for Long type. The default type for floating point literal is Double. Use f or F for Float type.

Use single quote for a char literal. It may use Unicode code point with prefix \u.

String literals use double quotes. Use """ for raw string. Use pipe | and method stripMargin to strip spaces in multi-line raw strings.

The expression in string interpolation is called a processed string literal. There are s string interpolator, raw string interplator (no escape) and f string interpolator (with % fomrmatter).

The compiler treats any expression consisting of an identifier followed by a string literal as a string intepolator expression and rewrite code as method calls.

Operators are Methods

Any method can be used in operator notation. Parameters are operands. More than one operands must be in parentheses.

Unary operator has a method name prefix of unary_. There are only four prefix operators: +, -, !, ~.

For a postfix oprator of a method without arguments, use () if it has a side effects and leave off () if it doesn’t have a side effects.

Use && and || for short-circuit; Use & and | without short-circuit.

The == calls equals method and can compare values of different types. Use eq and ne for reference equality.

Operator Precedence and Associativity

Precedence by the first char of an operator: * / %, + -, :, = !, <>, &, |, (letters), (all assignment operators).

Associativity is determined by the last char of an operator. : use right associativity.

All operands are evaluated from left to right.

If there are multiple operators, they are grouped by their associativity.

Rich Wrappers

Each basic type has a rich wrapper class that adds many methods to the basic type.