This lesson covers:

About this class

The first few weeks will cover basic syntax and concepts, then we'll start to open it up with more exercises.

Why Scala?

How Scala?

Think Scala

Scala is not just a nicer Java. You should learn it with a fresh mind; you will get more out of these classes.

Expressions

In []:
1 + 1
Out[]:
2

_1 is an automatically created value name given by the interpreter to the result of your expression. It has the type Int and contains the integer 2.

(Almost) everything in Scala is an expression.

Values

You can give the result of an expression a name.

In []:
val two = 1 + 1
Out[]:
2

You cannot change the binding to a val.

Variables

If you need to change the binding, you can use a var instead.

In []:
var name = "steve"
Out[]:
steve
In []:
name = "marius"
Out[]:
marius

Functions

You can create functions with def.

In []:
def addOne(m: Int): Int = m + 1

In Scala, you need to specify the type signature for function parameters. The interpreter happily repeats the type signature back to you.

In []:
val three = addOne(2)
Out[]:
3

You can leave off parens on functions with no arguments.

In []:
def three() = 1 + 2
In []:
three()
Out[]:
3
In []:
three
Out[]:
3

Anonymous Functions

You can create anonymous functions.

In []:
val f = (x: Int) => x + 1

This function adds 1 to an Int named x.

In []:
f(1)
Out[]:
2

You can pass anonymous functions around or save them into vals.

In []:
val addOne = (x: Int) => x + 1
In []:
addOne(1)
Out[]:
2

If your function is made up of many expressions, you can use {} to give yourself some breathing room.

In []:
def timesTwo(i: Int): Int = {
    println("hello world")
    i * 2
}

This is also true of an anonymous function.

In []:
(i: Int) => {
    println("hello world")
    i * 2
}
Out[]:
<function1>

You will see this syntax often used when passing an anonymous function as an argument.

Partial application

You can partially apply a function with an underscore, which gives you another function. Scala uses the underscore to mean different things in different contexts, but you can usually think of it as an unnamed magical wildcard. In the context of { _ + 2 } it means an unnamed parameter. You can use it like so:

In []:
def adder(m: Int, n: Int) = m + n
In []:
val add2 = adder(2, _:Int)
Out[]:
<function1>
In []:
add2(3)
Out[]:
5

You can partially apply any argument in the argument list, not just the last one.

Curried functions

Sometimes it makes sense to let people apply some arguments to your function now and others later.

Here's an example of a function that lets you build multipliers of two numbers together. At one call site, you'll decide which is the multiplier and at a later call site, you'll choose a multiplicand.

In []:
def multiply(m: Int)(n: Int): Int = m * n

You can call it directly with both arguments.

In []:
multiply(2)(3)
Out[]:
6

You can fill in the first parameter and partially apply the second.

In []:
val timesTwo = multiply(2) _
Out[]:
<function1>
In []:
timesTwo(3)
Out[]:
6

You can take any function of multiple arguments and curry it. Let's try with our earlier adder

In []:
(adder _).curried
Out[]:
<function1>

Variable length arguments

There is a special syntax for methods that can take parameters of a repeated type. To apply String's capitalize function to several strings, you might write:

In []:
def capitalizeAll(args: String*) = {
    args.map { arg =>
        arg.capitalize
    }
}
In []:
capitalizeAll("rarity", "applejack")
Out[]:
ArrayBuffer(Rarity, Applejack)

Classes

In []:
class Calculator {
    val brand: String = "HP"
    def add(m: Int, n: Int): Int = m + n
}
In []:
val calc = new Calculator
Out[]:
Calculator@e75a11
In []:
calc.add(1, 2)
Out[]:
3
In []:
calc.brand
Out[]:
"HP"

Contained are examples are defining methods with def and fields with val. Methods are just functions that can access the state of the class.

Constructor

Constructors aren't special methods, they are the code outside of method definitions in your class. Let's extend our Calculator example to take a constructor argument and use it to initialize internal state.

In []:
class Calculator(brand: String) {
    /**
     * A constructor.
     */
    val color: String = if (brand == "TI") {
        "blue"
    } else if (brand == "HP") {
        "black"
    } else {
        "white"
    }

    // An instance method.
    def add(m: Int, n: Int): Int = m + n
}

Note the two different styles of comments.

You can use the constructor to construct an instance:

In []:
val calc = new Calculator("HP")
Out[]:
Calculator@1e64cc4d
In []:
calc.color
Out[]:
black

Expressions

Our BasicCalculator example gave an example of how Scala is expression-oriented. The value color was bound based on an if/else expression. Scala is highly expression-oriented: most things are expressions rather than statements.

Aside: Functions vs Methods

Functions and methods are largely interchangeable. Because functions and methods are so similar, you might not remember whether that thing you call is a function or a method. When you bump into a difference between methods and functions, it might confuse you.

In []:
class C {
    var acc = 0
    def minc = { acc += 1 }
    val finc = { () => acc += 1 }
}
In []:
val c = new C
Out[]:
C@1af1bd6
In []:
c.minc // calls c.minc()
In []:
c.finc // returns the function as a value:
Out[]:
<function0>

When you can call one "function" without parentheses but not another, you might think Whoops, I thought I knew how Scala functions worked, but I guess not. Maybe they sometimes need parentheses? You might understand functions, but be using a method.

In practice, you can do great things in Scala while remaining hazy on the difference between methods and functions. If you're new to Scala and read [explanations of the differences](https://www.google.com/search?q=difference+scala+function+method"), you might have trouble following them. That doesn't mean you're going to have trouble using Scala. It just means that the difference between functions and methods is subtle enough such that explanations tend to dig into deep parts of the language.

Inheritance

In []:
class ScientificCalculator(brand: String) extends Calculator(brand) {
    def log(m: Double, base: Double) = math.log(m) / math.log(base)
}

See Also Effective Scala points out that a Type alias is better than extends if the subclass isn't actually different from the superclass. A Tour of Scala describes Subclassing.

Overloading methods

In []:
class EvenMoreScientificCalculator(brand: String) extends ScientificCalculator(brand) {
    def log(m: Int): Double = log(m, math.exp(1))
}

Abstract Classes

You can define an abstract class, a class that defines some methods but does not implement them. Instead, subclasses that extend the abstract class define these methods. You can't create an instance of an abstract class.

In []:
abstract class Shape {
    def getArea():Int    // subclass should define this
}
In []:
class Circle(r: Int) extends Shape {
    def getArea():Int = { r * r * 3 }
}
In []:
val s = new Shape
Out[]:
<console>:8: error: class Shape is abstract; cannot be instantiated
       val s = new Shape
               ^
In []:
val c = new Circle(2)
Out[]:
Circle@65c0035b

Traits

traits are collections of fields and behaviors that you can extend or mixin to your classes.

In []:
trait Car {
    val brand: String
}

trait Shiny {
    val shineRefraction: Int
}
In []:
class BMW extends Car {
    val brand = "BMW"
}

One class can extend several traits using the with keyword:

In []:
class BMW extends Car with Shiny {
    val brand = "BMW"
    val shineRefraction = 12
}

See Also Effective Scala has opinions about trait.

When do you want a Trait instead of an Abstract Class? If you want to define an interface-like type, you might find it difficult to choose between a trait or an abstract class. Either one lets you define a type with some behavior, asking extenders to define some other behavior. Some rules of thumb:

You are not the first person to ask this question. See fuller answers at stackoverflow:Scala traits vs abstract classes, Difference between Abstract Class and Trait, and Programming in Scala: To trait, or not to trait?.

Types

Earlier, you saw that we defined a function that took an Int which is a type of Number. Functions can also be generic and work on any type. When that occurs, you'll see a type parameter introduced with the square bracket syntax. Here's an example of a Cache of generic Keys and Values.

In []:
trait Cache[K, V] {
  def get(key: K): V
  def put(key: K, value: V)
  def delete(key: K)
}

Methods can also have type parameters introduced.

def remove[K](key: K)