Additional material for workshops introduction to scala

Variables

Lets start with basics. A variable in scala is declared like this:

1
var aVariable: String = "This is a variable"

Lets take a look at syntax:

  • we start with keyword var (as variable)
  • then after a colon we have a variable type
  • last but not least we have an equal sign (assignment operator) and the variable value. Important you cannot create a variable without initial value

What might be surprising we can omit the type and write the variable like this:

1
var aVariable = "This is a variable"

This is what is called type inference which means that the compiler will find the type of the variable.
What is important when you try to assign something that is not of the original type compilation will fail.

Values (might be called constant in other languages)

1
val aVariable: String = "This is a variable"

Values are similar to variables with the difference that once set their value cannot be changed. This is a thing called immutability.

Methods and functions

Like in other programming languages you can define a function (or a method). In scala we have two ways of declaring them:

1
def aMethod(argument: Int) : String = { argument.toString}

This way we declare a method called aMethod which accepts an argument called argument of type Int and returns a String which is a result of calling toString method. Please note that no return statement is required. In scala you always return last expression. Similarly to variables and values we can omit returned type because compiler will figure out what is returned basing on the last expression.

To call a method you can use () operator:

1
aMethod(1)

Default parameters are created with assignment operator like this:

1
def aMethod(argument: Int, argumentWithDefault: String = "Default variable"): String = ???

Scala allows you to use named parameters, which are extremely useful in case you have more than one default parameter:

1
2
3
4
5
6
7
8
9
10
11
def aMethod(arg1: Int, arg2: String = "Default parameter1", arg3: String = "Default parameter2"):String = {
s"arg1: $arg1 arg2: $arg2 arg3: $arg3"
}

aMethod(arg1 = 1, arg3 = "Not default parameter")
// will return
// arg1: 1 arg2: Default parameter1 arg3: Not default parameter

aMethod(arg1 = 1, arg2 = "Not default parameter"
// will return
// arg1: 1 arg2: Not default parameter arg3: Default parameter2

In scala you can also declare anonymous functions (sometimes called lambda)

1
val anonymousFunction: Int => String = { argument => argument.toString }

For case of simplicity lets suppose that methods and anonymous functions are equal. You can read about the differences here

Classes

In scala you can define a class using class keyword like this:

1
2
3
4
5
class Class1(constructorArg1: String, constructorArg2: Int) {
private val aClassVariable = constructorArg2.toString

def classMethod1 = constructorArg1
}

In order to create instance of this class you must pass all needed constructor variables using new operator:

1
val instance = new Class1("arg1", 2)

Constructor is a method so you can use everything I showed before(default parameters, calling parameters by name).

If we include val in front of constructor variable we will obtain a free getter:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Class2(val constructorArg: String) {
def method: String = s"This is constructor arg $constructorArg"
}

val newInstance2 = new Class2("arg")

newInstance2.method
// will return
// "This is constructor arg arg"

newInstance2.constructorArg
// will return
// "arg"

If we include var in front of constructor variable we will obtain a free getter and setter:

1
2
3
4
5
class Class3(var constructorArg: String)

val newInstance3 = new Class3("arg")
newInstance3.constructorArg //getter
newInstance3.constructorArg = "new value" //setter

We can also create a thing called case class which is an extremely popular concept in scala. Let me show you how it works:

1
2
3
4
5
6
7
case class ACaseClass(field1: String, field2: Int)
val caseClassInstance = ACaseClass("field1", 2) // notice no *new* !

caseClassInstance.field1
caseClassInstance.field2

val newCaseClassInstance = caseClassInstance.copy(field2 = 3)

If you want to read more about scala case classes take a look here