Learning Scala – day 1

I’ve decided to learn (at least the basics) of Scala. This post will cover the standard programming constructs and features, whilst not intended to be in depth, it will act as an entry into using Scala or a reminder for myself.

Note: I will be writing this post as somebody who comes from a C# background and has also used F#. So, sorry if you’re reading this and thinking “I don’t know what F# would look like” for example.

val and var

A val is immutable whereas a var is not. Hence assuming we have the following

object Calculator {
  def add(a: Int, b: Int): Int = {
      a + b
  }

  def subtract(a: Int, b: Int): Int = {
    a - b
  }
}

The we can assign our val thus

val calc = Calculator

// this line will error
calc = Calculator

whereas our var will allow reassignment, hence

var calc = Calculator

// this line will success
calc = Calculator

Simple enough.

Types

I’m not going to list all the types supported in Scala – ofcourse we have Int, String etc. What I will say though, is like F#, a type may be inferred or we might specify the type where a hint is required

Let’s see how we can use a type

val inferredString = "Hello"
val explcitString : String = "Hello"

We declare a type after the name of the val/var, as per the example above.

There’s an ultimate base type in Scala (a little like object in C#) and this is called Any. According to Unified Type System this Any type is not the same as the java.lang.Object. In Scala we have AnyRef which IS analogous to java.lang.Object whereas AnyVal is the equivalent base of value types. Ofcourse Any is the base of both AnyValue and AnyRef.

Like F#, Scala prefers the idea of a Nothing value than a null but (again like F#) null is also supported.

Scala, ofcourse, supports collections and arrays.

We can declare and array with type expected type or inferred, for example, these two arrays create the same type of array

// explicit
val a = Array[String]("a", "b")
// inferred
val b = Array("a", "b")

To access/alter an array at a given index we again use brackets (), i.e.

val a = Array("a", "b")
// amend "b" to "c"
a(1) = "c"

Return values

When a method returns a value we can use the return function, for example

def result : Int = {
   return(123)
}

or without parenthesis, i.e. return 123 or, like F# the return can also be the last line of the function, i.e.

def result : Int = {
   123
}

and with the type being inferred we can reduce this further to

def result = {
   123
}

Loops

The for loop, using the to function, for example

for(i <- 1 to 3) {
   print(i);
}

// will print
// 123

The for loop, using the until method

for(i <- 1 until 3) {
   print(i);
}

// will print
// 12

to and until are methods, hence we could use the following syntax

for(i <- 1 to 3) {}
for(i <- 1 to.(3)) {}
for(i <- 1.to(3)) {}

each of the above does the same thing, just with different syntax. What each of these methods does is return a Range, for example, in the the following code r is of type Range

val r = 1.to(3)

for(i <- r) {
   print(i)
}

It’s always worth noting the i is a val (hence immutable) type and is created each time through the loop.

Note: So how can “to” and “until” be used without parenthesis? If a method is zero or one arguments, we do not need the parenthesis. Therefore, if we have code like this

object Calculator {
  def inc(a : Int): Int = {
    a + 1
  }
}

// we can do this

val calc = Calculator
val r = calc inc 2

Back to loops, so we have a for loop, we’ve also got while loops so

var i = 0
while(i < 3) {
   print(i)
   i = i + 1
}

as well as do…while

var i = 0
do {
   print(i)
   i = i + 1
} while(i < 3)

like C#/Java we can also break out of a loop, however this keyword is not part of Scala. To use, we need to import the functionality using

import scala.util.control._

var i = 0
while(true) {
   print(i)
   i = i + 1
   if(i == 3)
      Breaks.break
}

Importing functionality/libraries

As you’ve seen with the Breaks.break method, we can import functionality as we do in C# with using and import in Java, like this

import scala.util.control._

The use of the _ is like a saying import everything within the scala.util.control namespace and we’ll then use code like Breaks.break to call the method, but we could also be more explicit and change our code to be like this

import scala.util.control.Breaks.break

// other code
if(i == 3)
   break

Like F# being built atop .NET and therefore allowing us access to a rich framework and eco-system, Scala is built upon Java and hence allows us access to it’s rich set of frameworks etc.

import java.util.Date

def output  = print(new Date() + ":" + msg)

in the above, it’s probably obvious, we’re using the Java Date class, we can import multiple specific classes like this

import java.util.{Date, Locale}

or for all (as already seen)

import java.util._

Classes and Objects

We can define a class as follows

class Report(msg: String) {

  def output = println(msg)
}

By default (i.e. without an scope declaration) classes are public, we could prefix the class thus private class Report to change from the default.

The msg: String is like a constructor (a similar design came up for Primary constructors in C# 6) but we can also create overload for the constructor like this

class Report(msg: String) {

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output = println(msg)
}

We can also define an object like this

object Report {

  def output(msg: String) = println(msg)
}

In scala this is known as a singleton object, I would say similar to a static class in C#. Hence does not have a constructor as we cannot create an instance of Report and we do not use the new keyword to use it, i.e.

val s = Report
s.output("Hi")

It’s also possible to associate an object with a class, for example

class Report(msg: String) {

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output  = Report output(this.msg)
}

object Report {
  def output(msg: String) = println(msg)
}

This pairing of class with an object is known as a companion class and vice versa we can think of the object as the companion object. In the example above, we’ve defined the equivalent of a static method in the object and the class output calls this. Obviously this is a slightly convoluted example, but another use is that the object might include (for example) private fields which are used from the class, like this

class Report(msg: String) {

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output  = print(Report.name + ":" + msg)
}

object Report {
  private def name = "Report"
}

alternatively, we can import the object and remove the need to prefix use of name with Report – like this

class Report(msg: String) {

  import Report._

  def this(msg: String, num: Int) = this(msg + "  " + num.toString)
  def this(num: Int, msg: String) = this(msg + "  " + num.toString)

  def output  = print(name + ":" + msg)
}

object Report {
  private def name = "Report"
}

Commenting code

As you’ve seen in some of the examples, scala supports // for single line comments as well as the standard (at least in C style languages) /* */

We can also support scaladoc comments, these use /** */ for example

/**
This is a report
*/
class Report(msg: String) {
}

In Intelli> (for example) selecting the Report when used in our code and pressing ctrl+q will result in a popup which will display the documentation for the class, the text with /** */ will be displayed as documentation.

Whoa, hold on, how about an application

So for all of the above, I’ve talked about the language, for these examples I’ve used ScalaTest but what about an entry point for an application.

Ofcourse we have main and we can create our application object like this

object MyApp {
  def main(args: Array[String]): Unit = {
     println("Hello World")
   }
}

As you’ll recall from the section on classes and objects, this is similar to a singleton class with, what we might view as, a static method.

This requires the args : Array[String]) and must return a void/Unit