Scala 3: The Dotty if/then/else-if/else/end syntax alvin January 27, 2020 - 11:45am
Table of Contents1 - Using `then` with if/else2 - Using `end if` with if/else3 - Closing a function with `end`4 - Discussion5 - Participate/contribute!6 - Attribution

As a brief note today, here’s an example of the Scala 3 “Dotty” if/then/else-if/else syntax, as used in a function:

def compare(a: Int, b: Int): Int =
    if a < b
    else if a == b
Back to top

Using `then` with if/else

You can also use the then keyword after your if expressions, if you prefer:

A Dart Future/then/catchError example

When I was trying to debug a problem that I thought was related to Flutter’s SharedPreferences library, I ended up rewriting a preferences method to use code like this rather than the normal approach:

Future<SharedPreferences> fPrefs = SharedPreferences.getInstance();
fPrefs.then((value) {rez = value.getBool(KEY_ENABLE_NOTIFICATIONS) ?? false; })
   .catchError((e) {
       debugPrint("===== ERROR: ${e.error}");
       return 60;
return rez;

While that ended up being a waste of time, the benefit of my side excursion is that I get to show this example of how to use then and catchError with a Dart future. So if you wanted to see a Dart Future/then/catchError example, I hope this is helpful.

How to write if/then/elseif/else in Drupal 8 Twig templates

As a quick note Drupal programming note, here’s an example of how to write if/then/elseif/else in Drupal 8 Twig templates:

{% if node.getType == 'photo' %}
{% elseif node.getType in ['book', 'page'] %}
{% else %}
{% endif %}

While I’m in the neighborhood, here are a few more if conditions I’ve written recently:

Scala: How to add ‘if’ expressions (guards) to match/case expressions

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.13, “How to add 'if' expressions (guards) to case statements.”


You want to add qualifying logic to a case statement in a match expression, such as allowing a range of numbers, or matching a pattern, but only if that pattern matches some additional criteria.


Add an if guard to your case statement. Use it to match a range of numbers:

How to use a Scala if/then statement like a ternary operator

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.6, “How to use a Scala if/then statement like a ternary operator.”


You’d like to use a Scala if expression like a ternary operator to solve a problem in a concise, expressive way.


This is a bit of a trick problem, because unlike Java, in Scala there is no special ternary operator; just use an if/else expression:

Scala control structure examples (if/then, match/case, for, while, try/catch)

This post contains a collection of Scala control structures examples. I initially created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe them much here, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).

if/then control structures:

Here are some examples of the Scala if/then control structure: