concept case - expression in category elm

appears as: case-expressions, case-expression, case-expression
Elm in Action

This is an excerpt from Manning's book Elm in Action.

Chapter 3 will add a powerful new conditional to our expression toolbox, one that has no analogue in JavaScript: the case-expression.

Refactoring an if-expression into a case-expression

We now have two conditionals that do nothing more than compare msg.description to a string. Let’s express this more cleanly by rewriting these if-expressions as a case-expression, as shown in figure 3.2.

Figure 3.2. Refactoring an if-expression into a case-expression

Whereas an if-expression is a two-way conditional, a case-expression is a multiway conditional. It lets us compare something to a wider range of values than just True or False.

Just as with a JavaScript switch-statement, we begin a case-expression by providing a value that will be run through a series of comparisons. Here we wrote case msg.description of because we want to run msg.description through these comparisons. Following the case are a series of branches such as the following:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest