Programming in Scala Gist 4

Vinu Charanya
vTechNotes
Published in
3 min readMay 25, 2016

These are the key points from the Programming in Scala, Second Edition by Martin Odersky, Lex Spoon, Bill Venners. I wanted to make note of some key points. They may seem broken and not make much sense, if you have not read the book. This is for personal reference. Feel free to correct me if I have interpreted anything wrong in this post.

Chapter 4 — Classes and Objects

4.1 Classes, Fields and Methods

  • Public is Scala’s default access level.
  • Method parameters in Scala are val meaning, they cannot be reassigned within the method.(This helps avoid the check whether some arg was reassigned)
  • In the absence of any explicit return statement, a Scala method returns the last value computed by the method. The recommended style is to have and avoid explicit or multiple return statements
  • A shorthand for methods is that you can leave off the
    curly braces if a method computes only a single result expression. If the
    result expression is short, it can even be placed on the same line as the def
    itself.
  • Procedure: A method that is executed only for its side effects
  • Unit is the default result type for functions without equals sign before the body of a function and any last statement will be converted to Unit though it is of different type
  • Adding an equals sign without an explicit return type will return the type of the last statement.

4.2 Semicolon Inference:
A semicolon is optional and required only if you write multiple statements in the same line.
The precise rules for statement separation are surprisingly simple for how well they work. In short, a line ending is treated as a semicolon unless one of the following conditions is true:

  1. The line in question ends in a word that would not be legal as the end of a statement, such as a period or an infix operator.
  2. The next line begins with a word that cannot start a statement.
  3. The line ends while inside parentheses (…) or brackets […], because these cannot contain multiple statements anyway.

4.3 Singleton Objects:

  • Classes in scala cannot have static members, instead it has singleton objects
  • Its definition is similar to class definition but it has keyword object
  • When it shares the name with a class, it becomes the class’s companion object and the class becomes the companion class. They can share each other’s private members.
  • A class can be instantiated, a singleton object cannot be instantiated and hence doesn’t take parameters.
  • Each singleton object is implemented as an instance of a synthetic class referenced from a static variable. The name of the synthetic class is the object name with a $ sign. Eg., CheckSumAccumulator$
  • A singleton object that doesn’t have a companion class is called a standalone object
  • A standalone object can be used to collect utils or defined with a main method of proper signature can be used as an entry point to a Scala application

NOTES:
The actual mechanism that the scala program uses to “interpret” a Scala source file is that it compiles the Scala source code to Java byte codes, loads them immediately via a class loader, and executes them.

4.4 A Scala Application

  • Scala class need not be placed in a file with the same class name
  • Scala has a compiler daemon called fsc (for fast Scala compiler)

4.5 The Application Trait

  • To use application trait, extend your singleton object with extends Application
  • Application trait declares a main method of the appropriate signature, which singleton object inherits.
  • The code between the {} is collected into a primary constructor of the singleton object and executed when the class is initialized.

Shortcomings of inheriting from Application trait.

  • Cannot access the command line args
  • Restriction in JVM threading model, multi threaded applications need explicit main method.
  • JVM do not optimize the initialization code of the an object executed by Application trait.
  • Inherit from Application trait only when your program is relatively simple and single-threaded.

--

--