Scala/Expressions, Not Statements

Simple Conditional Branching
Frequently, we need to make a simple decision regarding which value a variable should instantiate to. Take for example a somewhat artificial case:

At first sight it does not seem so bad. But we can do better if the branching itself evaluates a value.

In fact, Java/C# provides you with the (...) ? (...) : (...) syntax just because it's very handy at times.

Often this construct saves the Scala programmer from inventing a silly name for a short-lived variable. Perhaps it's equally important that it does not assign a silly value to it to start with.

Of course, if the value is needed more than once you probably should introduce a value. Even then,

This does not prevent you from using if ... else ... as statements. Statements, after all, are expressions.

Scaling It Up
Understandably if ... else if ... else ... chains are just as common, and they work just as fine in Scala. We might, for a finer result, try,

But more than often we need to branch on values rather than conditions. Basically that's why we have switch in Java/C#. Scala uses match for this,

Several things look different. For one, these cases do not use break to indicate the end of a branch. You don't have a fall through. Not that you need to fall through. They make programs harder to read and reason about. You can, however, group equivalent cases so that the principal use of falling through switch statements can be handled. Although Scala does not have the default keyword, the underscore reads "anything else", so to speak.

If you, for some reason, try this in Java 1.6 or lower, you'll probably notice another difference. <tt>switch</tt> only handles primitive types (and <tt>String</tt> since Java 1.7) so a straightforward translation will have to use <tt>if</tt> chains with a couple of <tt>equals</tt> calls. Scala uses <tt>equals</tt> to match cases for objects to make the code cleaner to read.

We will have to say more on <tt>match</tt> here and there in this book. For now, you probably will appreciate the ability to mix conditionals and cases to handle very special cases, like,

Although to make sense, this case should sit above the default case.

Trying And Failing
Even a <tt>try ... catch ...</tt> block is an expression in Scala and this can result in remarkably readable code:

As an aside, note that <tt>catch</tt> looks very much like <tt>match</tt> (and the underscore here means we don't care which specific exception was thrown).