Variables

The single equals sign is not assignment in Elixir. It is the match operator: the value on the right is matched against the pattern on the left. A fresh name on the left matches any value, and the match binds that name to it. The distinction sounds academic until the first pattern of several names, and it is the reason this chapter comes before functions and pattern matching.

= is a match

With a name on the left, a match is a binding: the name did not exist before, so it has nothing to compare against and takes the value. The script below binds two names and prints a line that uses both:

bind.exs

first_name = "Ada" age = 36 IO.puts("#{first_name} is #{age}")

elixir bind.exs

Ada is 36

A name may hold any value: a binary, an integer, a list, a function. There is no declaration and no type in front of the name, because the value carries its type, as the previous lesson showed.

A name can be rebound

A match against a name that already exists binds it again. The old value is not changed, because values never change; the name simply points at a new value. This is why a chain of small steps reads naturally, each line giving the name a new value:

rebind.exs

x = 1 x = x + 1 x = x * 10 IO.puts(x)

elixir rebind.exs

20

A reader from a language with mutation sees an assignment; the difference is that the first 1 still exists as a value and nothing can change it. A function that received it keeps its own copy, and a parallel process cannot see the new binding. Rebinding is local to the function and to the process that runs it.

Names in snake_case

A name starts with a lowercase letter or an underscore, and the words inside it are separated by underscores: first_name, total_count, max_retries. Elixir has no other variable style, and the formatter does not change the case of a name, so a badly named binding stays badly named.

A name that starts with an underscore tells the compiler the value is not used on purpose. _result silences the unused-variable warning, while a name without the underscore that is never read is a warning. A capital letter is not an option: Name is an alias of a module, and the compiler rejects it as a variable.

The pin keeps a value

A match against a bound name normally rebinds it. The pin operator ^ turns the name into a value and asks the match to compare instead: the line passes only when the right side is exactly what the name holds. The script below matches a pair of names and then checks a value against a binding:

match.exs

{first, second} = {1, 2} IO.puts(first + second) expected = 42 ^expected = 42 IO.puts("the pin matched")

elixir match.exs

3
the pin matched

The first line destructures a tuple: two names on the left take the two values on the right. That works for any shape, which is the subject of the pattern matching lesson; the pin is enough here.

The mistake: a pin that does not match

A pin does not rebind and does not convert. When the value differs, the match fails and the program stops with a MatchError that names the value it could not match:

pin.exs

expected = 42 ^expected = 43

elixir pin.exs

** (MatchError) no match of right hand side value:

    43

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

The blank lines and the indent are part of the report: the value sits on a line of its own. The fix depends on the intent: drop the pin to rebind, or fix the right side so that it equals the pinned value. A pin in a function head is how a function refuses an argument that is not the value it expects, and the pattern matching lesson comes back to that form.

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 the = sign do in Elixir?

    Answer

    It matches the value on the right against the pattern on the left

    A match binds a fresh name or fails; comparison is ==, and there is no declaration.

  2. What happens when a bound name is matched again without a pin?

    Answer

    The name is bound to the new value

    A name rebinds on every match; the pin is what makes it compare instead.

  3. What does the pin operator ^ do?

    Answer

    It matches against the value the name already holds

    The pin turns the name into a value, so the match has to equal it.

  4. Which name follows the naming style of Elixir?

    Answer

    total_count

    Names are lowercase words joined by underscores; a capital letter would make an alias.

  5. What does this program print? x = 1 x = 2 IO.puts(x)

    Answer

    2

    The second match rebinds x, and the print reads the latest binding.

  6. What does this program print? {first, second} = {1, 2} IO.puts(first + second)

    Answer

    3

    The pattern takes the two elements of the tuple, and their sum is 3.

  7. What does this program print? x = 5 ^x = 5 IO.puts("matched")

    Answer

    matched

    The right side equals the pinned value, so the match passes and the line prints.

  8. What does this program do? x = 5 ^x = 6

    Answer

    A MatchError, because 6 is not the pinned value 5

    The pin makes the line a comparison, and two different integers do not match.

  9. What does a name starting with an underscore, such as _result, tell the compiler?

    Answer

    The value is intentionally unused, so no warning is due

    An underscore prefix suppresses the unused-variable warning.

  10. What does this program print? _name = "Ada" IO.puts("done")

    Answer

    done

    The underscored binding is allowed to go unread and does not stop the print.

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