Expressions, Values, and Types in Scala

March 5, 2017

Scala is a wonderful programming lanugage that combines functional and object oriented principals. It compiles to Java byte code and thus runs on the JVM. Here are some of my favorite things about Scala!

  • Can utilize all of Java libraries
  • Extreme freedom to program elegantly in OO or functional style
  • Strongly typed
  • Expressive

The list goes on. If you're here you've most likely decided to learn Scala so I won't waste your time. This blog series will aim to help you understand Scala, not just show an example and a result. I'm a firm believer that understanding the code you write is essential to becoming a successful developer. With that said, I hope you enjoy this blog series!

Expressions and Values

Expressions in Scala is the text of the program. Expressions evaluate to a value. Expressions exist at compile time and has type information. Values are the programs representation in memory. Values do not have type information! Values is literally just bits of data saved into a computers memory.. think 0's and 1's. Lets look at an example.

scala> "Hello World!"  // Expression
res1: String = Hello World!  // Value

Okay, so what happened.

1) Typed the expression "Hello World!"

2) Hit enter

3) Scala evaluated this program. Scala is telling us the type of the expression, which is a String, and telling us what the value is - Hello World!

Note that the value is the same as the expression. When this happens, this is called a literal expression. There are many other literal expressions in Scala. Integers and booleans are obvious ones.

scala> 1
res2: Int = 1
scala> true
res3: Boolean = true

Block Expressions

It is often useful to put expressions onto multiple lines. Scala allows us to do this with curly braces. This expression contains other expressions which all evaluate to a single value. The last expression in the block is what is returned.

    ???  // gets returned

to illustrate, we might have a block expression which looks like this

    "Hello World!"

which will evaluate to the value Hello World!.

Lastly, it is important to know that expressions have their own scope. Here’s an example

    val a =1
    val b = {
        val a = 5
        a  // 5

This expression will evaluate to 1, since a was redefined in a different scope!

Conditional Expressions

Conditional expressions lets programmers choose which expressions to evaluate based on a condition. The condition is an expression itself which must evaluate to a boolean, i.e. any expression that evaluates to true or false.

if (condition) {
    // Expression
} else if (condition) {
    // Expression
} else {
    // Expression

Well that is pretty standard. Remember when I said the condition is just an expression? Hmm, well then an expression block should work in there? Lets try it out.

if ({
    println("checking the if condition!")
}) {
// checking the if condition
// neat!

It is important to remember the fundamentals. While you'll likely never do this, I hope it illustrates how truly understanding the code allows you to be creative with your solutions.

These control flow of this is pretty common to most languages so I won’t go into the logical flow too much, but please note that after the if (condition) there is an expression block. This is important for a couple reasons.. take a look at the following code.

val a = 1
val b = if (true) {
    val a = 3

What do you think println(a) is going to print? Remember, expressions have their own scope. Also, an expression only returns a value. Everything else in the if block won’t do anything unless it changed outside code (e.g. updating a database). The output of this program is…


Well that is anticlimactic, but too bad. Scala’s awesome. Try it out for yourself in the shell. the value a was reassigned in a different scope. Notice that b is equal to the value Done. This is key. Don’t forget that the code after the condition is just a block expression being executed, so only the last expression is returned as the value for the entire block expression.


Okay! We’ve made it this far. And it has been hard. Why - you ask? - Because it is hard to talk about Scala without knowing what types are. As I mentioned earlier, Scala is a definitely typed language. This means that the types of objects need to stay consistent throughout our code. Types optionally allow us to place further restriction on our own software in order to write more reliable code. It also serves as excellent documentation to make reading code to understand its purpose extremely quick. This is great for reading other developers code, and even code you’ve written yourself and forgotten about!

Here is a quick example:

val a: String = 1
found :Int(1)
required: String
    val a: String = 1

We are trying to create a value a and assign it to the literal expression 1. We restrict a to be a string (I’ll go into the syntax in a future lesson, so don’t worry about that for now) and attempt to assign it to 1, an integer literal expression. Scala sees that the types do not match and throws the error. Scala even goes as far as to point to where we messed up! very cool.

We can also define our own types.

// Define your own types!
class Dog(val breed: String)
defined class Dog

val lab: Dog = new Dog("Labrador")
lab: Dog = Dog@6caa4dc5

Again, I’ll be going over exactly what I just did more in future lessons if you’re new to programming, but just note for now that I created a new type Dog, and when I created a value lab I was able to restrict its value to that type.

I've written a video corresponding to this blog here:

Comment Enter a new comment: