Truthiness: only false and nil

A condition in Elixir accepts any value, and the rule that decides whether it passes is one line long: false and nil are falsy, and every other value is truthy. There is no separate list of false values for zero, the empty string, the empty list or the empty map, which is the difference a reader from another language trips on first.

Only false and nil are falsy

The ! operator turns a value into its boolean opposite, so it is the shortest way to see the rule. Two values answer true, and everything else, a string and a number included, answers false:

falsy.exs

IO.puts(!false) IO.puts(!nil) IO.puts(!true) IO.puts(!"hello") IO.puts(!0)

elixir falsy.exs

true
true
false
false
false

A double !! turns any truthy value into true and a falsy one into false, which is how a program normalises a value to a boolean. !nil is true because nil is the atom :nil, and the rule names that atom rather than "nothing".

Zero, empty and everything else

A zero is a number, so it is truthy. An empty string is a binary, an empty list is a list and an empty map is a map; all three are values, and all three pass a condition. Each line below takes its branch:

truthy.exs

if 0, do: IO.puts("zero is truthy") if "", do: IO.puts("the empty string is truthy") if [], do: IO.puts("the empty list is truthy") if %{}, do: IO.puts("the empty map is truthy")

elixir truthy.exs

zero is truthy
the empty string is truthy
the empty list is truthy
the empty map is truthy

Emptiness is a property to ask about, not a truth value: String.length("") == 0, Enum.empty?([]) and map_size(%{}) == 0 answer it. A program that writes if list do to mean "if the list has something" is wrong for the empty list, and the condition lesson comes back to the same point.

or and and return a value

|| returns its first truthy value, or the last value when none is truthy. && returns its first falsy value, or the last value when none is falsy. This is why || is the idiom for a default, and why the idiom does not fire for a zero or an empty string:

default.exs

input = nil IO.inspect(input || "guest") IO.inspect(0 || "guest") IO.inspect("" || "guest") IO.inspect(nil && "never")

elixir default.exs

"guest"
0
""
nil

The second line is the trap: a count of zero is a real value, so the default does not replace it. When zero must trigger the default, test for it: if input == nil or input == 0, or a function that says what a missing value is. The functions chapter writes that function with pattern matching.

The mistake: and needs booleans

|| and && accept any value, but or and and are strict: both sides must be true or false. An integer on the left stops the program with a BadBooleanError:

and.exs

IO.puts(1 and true)

elixir and.exs

** (BadBooleanError) expected a boolean on left-side of "and", got:

    1

    and.exs:1: (file)
    (elixir 1.19.7) lib/code.ex:1613: Code.require_file/2

The report puts the value on its own line, and the first line names the operator and the side that was wrong. Use && when the values may be anything, and and when a boolean is the contract. The same split holds for ! and not: !0 is false, while not 0 raises an ArgumentError.

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. Which values are falsy in Elixir?

    Answer

    Only false and nil

    The rule names two values; zero, an empty string and an empty collection are truthy.

  2. What does if 0 do do?

    Answer

    It runs the branch, because zero is truthy

    Only false and nil are falsy, and 0 is an integer.

  3. What does || return?

    Answer

    The first truthy value, or the last value when none is truthy

    || is a value operator, which is what makes the default idiom work.

  4. What does and require that && does not?

    Answer

    A boolean on each side

    and and or are strict boolean operators; && and || accept any value.

  5. What does this program print? IO.inspect(nil || "fallback")

    Answer

    "fallback"

    nil is falsy, so || moves to the right side and returns it.

  6. What does this program print? IO.inspect(0 || "fallback")

    Answer

    0

    Zero is truthy, so || returns it and never reads the default.

  7. What does this program print? IO.inspect(false && "yes")

    Answer

    false

    && returns its first falsy value, which is false here.

  8. What does this program print? IO.puts(if 0, do: "yes", else: "no")

    Answer

    yes

    The condition is 0, which is truthy, so the do branch runs.

  9. What does this program do? IO.puts(1 and true)

    Answer

    A BadBooleanError: and needs a boolean on the left

    The strict and operator refuses an integer where a boolean belongs.

  10. What does this program do? IO.puts(not 1)

    Answer

    An ArgumentError: not needs a boolean

    not is the strict negation; ! is the truthiness one and accepts any value.

Автор статьи: Arthur Isaev