Learn Scala 3 Fast: Expression-Oriented Programming

if statements give us our first opportunity to introduce something known as Expression-Oriented Programming, or EOP.

Statements vs expressions

One of the terrific things about Scala is that every line of code can be an expression, and not just a statement.

In programming, a statement is a block of code that does not return a result, and is used solely for its side effect. For instance, this line of code is a statement because it does not return a result:

if a == b then println(a)

Lines of code like that are used solely for side effects, and in this case the side effect is printing to STDOUT.

Conversely, an expression is a block of code that does return a result, and typically has no side effects. In this lesson I show how to use the if/then construct as an expression.

Technically it’s more accurate to say that the previous if/then example does not return a useful result. It returns something — a type called Unit, which is like void in other languages — but we generally don’t care about it.

Using ‘if’ as an expression

A great thing about Scala is that each of its constructs can be used as an expression. This includes the if/then construct.

What this means is that the if/then construct returns a result, and you can use that result, such as assigning it to a variable, like this:

val c = if a < b then a else b

For some people that code can be easier to read if it’s on multiple lines, so you can also write it like this:

val c =
    if a < b then a else b

or this:

val c = 
    if a < b then
        a
    else
        b

All of those examples return the same result, and can be read as, “If a is less than b, assign the value of a to c. If not, assign the value of b to c.”

If you’re familiar with the ternary operator syntax in Java and other languages, you can see that there is no need for a special syntax in Scala: you just write a normal if/then expression.

Preview: Using if/then as the body of a method

As a quick peek into the future, as you’ll see in future lessons, because the if/then construct is an expression, you can also use it as the body of a Scala function, like this:

def min(a: Int, b: Int): Int =
    if a < b then a else b

That code defines a function named min that returns the minimum value of the two integer parameters that are passed into it, a and b.

For the purposes of this lesson, the important thing is that if/then is an expression and returns a value, and because of this it can be used as the entire body of a function. This is one of the beautiful things about EOP, and you’ll see much more of this in the lessons that follow.

Finally, even though I haven’t introduced functions yet, if you have experience with other programming languages, I suspect that you may understand how that function works. You can see it in action in these examples:

println(min(1, 2))      // prints "1"

val x = min(1, 1_000)   // x is assigned the value 1

As you’ll see throughout this book, EOP is another feature that makes Scala concise and readable, i.e., expressive.

Exercises

The exercises for this lesson are available here.

books by alvin