Skip to main content

Scala

I'll start with a disclaimer:

  • These are my notes, thus some level of basic programming knowledge is required
  • I cannot possibly cover every unique feature of Scala, only what's most important to me
  • For a better tutorial check out the Scala docs or RocktheJVM

Also I'll be focusing on Scala 2, rather than the most recent release of Scala 3. There are big differences in the syntax but the functionality remains mostly the same. My reasoning for this is the Government dataset I use is frozen on Scala 2.12.15. More tech-fluent companies may be using Scala 3, but those of us in healthcare and government should get comfortable with v2.

A Brief Overview of Types

Scala has types, similar to most other JVM based languages. However, one thing that is specific to Scala is starting variable declaration with val or var.

val someValue: Int = 42 // Immutatble
var someBool = false // mutable, type inferred

val is similar to final in Java, the variable is now a constant. While var can be reassigned to the same type. Type is usually optional as it can be inferred, but it's often recommended to use the type for ease of reading.

Scala is a functional programming language. While in most languages we think about the flow of instructions in sequence, but in a functional language we want to think in terms of composing the values through expressions. For example, observe the if expression below:

val ifExpression = if (testValue > 42) "String1" else "String2" // if-expression

This is much more readable than other ternary operators in other langauges, this allows for cleaner chaining:

val ifExpression = if (testValue > 42) "String1" 
	else if (testValue == 0) "String2"
    else "String3" // chained if-expression