Comments can be quite useful for:

  • Providing documentation that explains how the code works.
  • Preventing execution of some code when we are experimenting or trying to find the root cause of a bug.

Like most programming languages, Elm provides two types of comments: single line and multi-line.

Single Line Comments

Single line (also known as inline) comments start with --. Elm will ignore any text between -- and the end of the line.

> 42 -- This is a single line comment and will be ignored by Elm.

> 42 * (78 - (50 / 4) + (80 / 3)) -- A slightly more complex math expression.

Multi-line Comments

Multi-line (also known as block) comments start with {- and end with -}. Elm will ignore any text between {- and -}.

> 5 {- This is a block comment and will be ignored by Elm. -}

Here is an example of a multi-line comment that explains how the || operator works in Elm.

{- The logical OR operator. `True` if one or both inputs are `True`.

  Note: When used in the infix position, like `(left || right)`, the
        operator short-circuits. This means if `left` is `True` we do
        not bother evaluating `right` and just return `True` overall.

(||) : Bool -> Bool -> Bool
(||) =

Generally speaking it’s unnecessary to type comments in the repl. They are more appropriate for the code written in a file.

Toggling Comments

When we are experimenting with code, we often want to toggle comments. Most code editors provide a shortcut for toggling single line comments. For example, in Sublime Text and Atom a comment can be toggled (on a Mac) by pressing Cmd + /. However, these editors don’t have a good mechanism for toggling multi-line Elm comments. One handy trick we can use for that is to add and remove } on the first line. Let’s say we have some code commented out like this:

  multiply x y = x * y
  divide x y = x / y

Your source editor might not know how to toggle Elm comments out-of-the-box. You might have to install a plugin for that. Refer to the Source Code Editor section in Chapter 2 to find out how to install an Elm plugin for your favorite editor.

Notice we added extra - after {- and before -}. Now to uncomment the code all we have to do is add } to the end of the first line.

  multiply x y = x * y
  divide x y = x / y

Because the leftover } at the end is preceded by --, Elm treats it as a single-line comment. To bring back the multi-line comment, just remove } from the first line.

Back to top

New chapters are coming soon!

Sign up for the Elm Programming newsletter to get notified!

* indicates required