cond

In Elixir, cond takes a list of conditions and runs the body of the first one that is truthy. It is the form for a decision whose branches are tests of their own, not shapes of one value: a score in a range, a temperature above a limit, a file that may or may not exist. Where case asks "what is this value", cond asks "which of these statements is true".

A chain of conditions

Each clause is a condition, an arrow and a body. The clauses are tried from the top, and the first condition that is truthy decides the result. The conditions follow the truthiness rule: only false and nil fail, so a comparison that returns false moves on to the next clause:

grade.exs

score = 85 label = cond do score >= 90 -> :excellent score >= 70 -> :good score >= 50 -> :pass true -> :fail end IO.puts(label)

elixir grade.exs

good

The order matters for the same reason it does in a case: 85 is also above 50, but the second clause is reached first and wins. A chain is easiest to read when the conditions narrow from the strictest to the loosest, or from the most common case to the rarest.

The true clause

A final clause whose condition is true is the fallback. It always runs when the clauses above it were false, which is the role else plays in an if:

sign.exs

n = -3 kind = cond do n < 0 -> :negative n == 0 -> :zero true -> :positive end IO.puts(kind)

elixir sign.exs

negative

The three conditions are independent tests of the same number, which is exactly what cond is for. A case would have to write the number three times in its patterns, and the reader would have to reconstruct the ranges. The true clause is written as a condition rather than a keyword so that the syntax stays one shape.

cond returns a value

Like if and case, cond is an expression. The value is the body of the clause that ran, so a function can return a cond directly:

value.exs

defmodule Price do def label(cents) do cond do cents == 0 -> "free" cents < 100 -> "under a dollar" true -> "a dollar or more" end end end IO.puts(Price.label(0)) IO.puts(Price.label(75)) IO.puts(Price.label(250))

elixir value.exs

free
under a dollar
a dollar or more

The truthiness rule explains the form: a condition that is a number is always truthy, so cond do 0 -> ... would take that clause for every value. Every condition here is a comparison or the literal true, which is what a reader expects.

When case reads better

A cond whose every clause names the same value with a different comparison is a case in disguise, and a case states the value once. For a result tuple, a message tag or a list shape, use case: the patterns show the possible shapes, and the compiler can warn about a clause that can never match. For unrelated statements, use cond. Neither form replaces a function with several clauses when the decision is part of the contract of the function.

The mistake: no clause is true

A cond with no true clause assumes that one condition always holds. When none does, the program stops with a CondClauseError and names no value, because there was no value to match:

nofallback.exs

n = 5 cond do n > 100 -> IO.puts("large") end

elixir nofallback.exs

** (CondClauseError) no cond clause evaluated to a truthy value
    nofallback.exs:4: (file)
    (elixir 1.19.7) lib/code.ex:1613: Code.require_file/2

The report gives the line of the end and the stack, and the first line says everything: no clause was true. Add the true clause, or widen the conditions, and every value has a clause. A cond over input the program does not control always needs the fallback; one over a value the program just tested may leave it out on purpose, and then the crash is the report of a case that should not happen.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does cond do with its clauses?

    Answer

    It runs the body of the first clause whose condition is truthy

    The clauses are conditions, and the first truthy one decides the result.

  2. Which clause is the usual fallback of a cond?

    Answer

    A final true -> clause

    cond has no else and no underscore pattern; the condition true is always truthy.

  3. What happens when no condition of a cond is truthy?

    Answer

    It raises CondClauseError

    A cond with no true condition is a run-time error, which is why the fallback is common.

  4. When is case the better form?

    Answer

    When the decision is about the shape of one value

    case matches one value against patterns; cond chains independent conditions.

  5. What does this program print? IO.puts(cond do 1 > 2 -> :a true -> :b end)

    Answer

    b

    The first condition is false, and the true clause is the first truthy one.

  6. What does this program print? IO.puts(cond do false -> 1 nil -> 2 true -> 3 end)

    Answer

    3

    false and nil are the only falsy values, so the third clause is the one that runs.

  7. What does this program do? cond do false -> 1 end

    Answer

    A CondClauseError: no condition was truthy

    The only condition is false, so no clause runs and the expression raises.

  8. What does this program print? x = cond do true -> 42 end IO.puts(x)

    Answer

    42

    The body of the true clause is the value of the cond, and it is assigned to x.

  9. What does this program print? IO.puts(cond do 0 -> :zero true -> :other end)

    Answer

    zero

    Zero is truthy under the rule of the language, so the first clause runs.

  10. In which order are the conditions of a cond tried?

    Answer

    From the first clause in the file to the last

    The runtime walks the clauses in source order and runs the first truthy one.

Article author: Arthur Isaev