Values and types

Every value of Elixir has a type, and the type belongs to the value, not to the name it is bound to. There is no declaration and no type annotation on a variable: the program carries a value, and the runtime knows what it is. This lesson tours the types a beginner meets, shows the functions that ask a value what it is, and ends with the error that mixing two types produces.

A value, a type

The literals of the language are short. A number without a point is an integer, a number with a point is a float, and a word with a colon is an atom. Double quotes make a binary, square brackets a list, braces a tuple, and %{ and } a map:

Value Type The check that answers true
42 integer is_integer/1
3.14 float is_float/1
:ok atom is_atom/1
"hello" binary is_binary/1
[1, 2] list is_list/1
{1, 2} tuple is_tuple/1
%{a: 1} map is_map/1
fn x -> x end function is_function/1
self() pid is_pid/1

The checks are ordinary functions that return true or false. A program can call them on any value, and that is how a function decides what it was given. The script below asks about one value of each type and prints the answer:

types.exs

IO.puts("42 is an integer: #{is_integer(42)}") IO.puts("3.14 is a float: #{is_float(3.14)}") IO.puts(":ok is an atom: #{is_atom(:ok)}") IO.puts("hello is a binary: #{is_binary("hello")}") IO.puts("[1, 2] is a list: #{is_list([1, 2])}") IO.puts("{1, 2} is a tuple: #{is_tuple({1, 2})}") IO.puts("%{} is a map: #{is_map(%{a: 1})}")

elixir types.exs

42 is an integer: true
3.14 is a float: true
:ok is an atom: true
hello is a binary: true
[1, 2] is a list: true
{1, 2} is a tuple: true
%{} is a map: true

A variable does not remember a type, because it does not carry one: the value does. Bind x to 42 and the type of x is integer; rebind it to "42" and the type is binary. The language checks each operation on the value itself, at the moment the operation runs.

Ask a value what it is

The is_* family is bigger than the table. A few checks answer questions that are easy to get wrong: is_number/1 is true for an integer and for a float, is_boolean/1 is true only for true and false, and is_nil/1 is true only for nil. An atom, a binary and a list are never numbers, however much their text looks like one:

numbers.exs

IO.puts(is_number(42)) IO.puts(is_number(3.0)) IO.puts(is_number("42"))

elixir numbers.exs

true
true
false

A check on a value that is not there is a common source of confusion: nil is an atom, so is_atom(nil) is true and is_nil(nil) is true as well. The truthiness lesson explains why nil still behaves like a missing value in a condition.

inspect and the shape of a value

inspect/1 returns the text a developer reads: the value in the notation of the language, with quotes around a binary and the keys of a map in place. IO.inspect/1 prints that text, and IO.puts/1 prints a binary without quotes. The difference matters the moment a value is not a binary:

inspect.exs

value = %{name: "Ada", tags: [:math, :code]} IO.puts(inspect(value)) IO.inspect(value) IO.puts(inspect({:ok, 42})) IO.puts(inspect(3.0))

elixir inspect.exs

%{name: "Ada", tags: [:math, :code]}
%{name: "Ada", tags: [:math, :code]}
{:ok, 42}
3.0

The last line shows why the integers and the floats are separate types: 3.0 keeps its point, and a program that prints it does not turn it into 3. inspect/1 is also the function behind the shell output of the previous chapter, so a value that looks right in iex looks the same when printed with it.

The mistake: mixing types

Elixir does not convert a value to make an operation fit. Adding a binary to an integer is not a warning and not a silent conversion; it is an ArithmeticError, and the program stops at that line:

mixed.exs

IO.puts(1 + "1")

elixir mixed.exs

** (ArithmeticError) bad argument in arithmetic expression: 1 + "1"
:erlang.+(1, "1")
mixed.exs:1: (file)

The message names the operator, the two values and their order, which is usually enough to see which side is wrong. The fix is an explicit conversion: String.to_integer("1") before the addition, or Integer.to_string(1) before the join. The numbers lesson has both calls.

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. Where does the type of a value live in Elixir?

    Answer

    In the value itself, checked at run time

    There is no type declaration: a binding holds a value, and the value knows its type.

  2. Which check is true for both 42 and 3.0?

    Answer

    is_number/1

    is_number/1 accepts integers and floats; the other checks accept one type each.

  3. What does inspect/1 return?

    Answer

    A binary with the text of the value as a developer reads it

    inspect/1 returns a binary, and IO.inspect/1 is the call that prints it.

  4. What is true about nil?

    Answer

    It is an atom, and is_nil/1 is true for it

    nil is the atom :nil, so is_atom/1 is true and is_nil/1 recognises it.

  5. What does this program print? IO.puts(is_binary("hi"))

    Answer

    true

    A double-quoted literal is a binary, so is_binary/1 returns true.

  6. What does this program print? IO.puts(is_integer(1.0))

    Answer

    false

    1.0 is a float; is_integer/1 is false for every float.

  7. What does this program print? IO.puts(inspect({:ok, 42}))

    Answer

    {:ok, 42}

    inspect/1 writes the tuple in its literal notation, braces and colon included.

  8. What does this program print? IO.puts(is_atom("ok"))

    Answer

    false

    The quoted value is a binary; the atom would be written :ok.

  9. What does this program do? IO.puts(1 + "2")

    Answer

    An ArithmeticError: the operands are an integer and a binary

    The + operator adds numbers only and does not convert a binary.

  10. What does this program do? IO.puts(String.length(42))

    Answer

    A FunctionClauseError, because String.length/1 needs a binary

    No clause of String.length/1 accepts an integer, so the call raises FunctionClauseError.

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