TExcelTAD
Home › Elixir › Elixir Basic Types

Elixir Basic Types

Elixir has a small set of built-in types. Every value belongs to exactly one of them, and there is no "null pointer" hiding in your variables: nil is just an atom.

Type Examples Notes
integer 42, -7, 1_000_000, 0xFF, 0b1010 arbitrary precision: never overflows
float 3.14, 1.0e-3 64-bit double precision; needs a digit on both sides of the dot
boolean true, false actually the atoms :true and :false
atom :ok, :error, :red a constant whose value is its own name
string "héllo" UTF-8 encoded binary, double quotes
list [1, 2, 3] linked list
tuple {:ok, 5} fixed-size, stored contiguously
map %{name: "Ada"} key-value store
function fn x -> x * 2 end functions are values too
nil nil the atom :nil, means "no value"

Numbers

Integers can be as big as you like. The / operator always returns a float; use div/2 and rem/2 for integer division and remainder:

elixir · try it yourself
IO.inspect(2 ** 100)
IO.inspect(10 / 2)
IO.inspect(div(17, 5))
IO.inspect(rem(17, 5))
IO.inspect(1_000_000 + 0xFF)
IO.inspect(round(2.5))
IO.inspect(trunc(-3.9))
IO.inspect(Float.round(3.14159, 2))
1267650600228229401496703205376
5.0
3
2
1000255
3
-3
3.14

2 ** 100 is a 31-digit number and Elixir prints it exactly. round/1 rounds half away from zero, so round(2.5) is 3.

Atoms and booleans

An atom is a named constant. Atoms are used everywhere as tags and options: :ok, :error, :not_found. true, false and nil are atoms written without the colon.

elixir · try it yourself
status = :ok
IO.inspect(status)
IO.inspect(status == :ok)
IO.inspect(is_atom(true))
IO.inspect(true == :true)
IO.inspect(is_atom(nil))
:ok
true
true
true
true

Module names such as String are atoms too (String is the atom :"Elixir.String"). That is why you can store a module in a variable and call functions on it.

Strings

Strings are written in double quotes and are UTF-8. Interpolation with #{} works inside them, and <> joins two strings:

elixir · try it yourself
city = "São Paulo"
IO.puts("Welcome to " <> city)
IO.puts("#{city} has #{String.length(city)} characters")
IO.inspect(byte_size(city))
IO.inspect(String.upcase(city))
Welcome to São Paulo
São Paulo has 9 characters
10
"SÃO PAULO"

String.length/1 counts characters (9) while byte_size/1 counts bytes (10), because ã takes two bytes in UTF-8. The Strings lesson goes deeper.

Checking types

Elixir has a family of is_* functions. They are often used in guards, which you will meet when writing functions:

elixir · try it yourself
values = [42, 3.5, "text", :atom, [1], {1, 2}, %{}, nil]

for v <- values do
  kind =
    cond do
      is_integer(v) -> "integer"
      is_float(v) -> "float"
      is_binary(v) -> "string (binary)"
      is_boolean(v) or is_nil(v) -> "boolean or nil"
      is_atom(v) -> "atom"
      is_list(v) -> "list"
      is_tuple(v) -> "tuple"
      is_map(v) -> "map"
    end

  IO.puts("#{inspect(v)} is #{kind}")
end
42 is integer
3.5 is float
"text" is string (binary)
:atom is atom
[1] is list
{1, 2} is tuple
%{} is map
nil is boolean or nil

The order of the cond branches matters: nil is also an atom, so it must be checked before is_atom/1. There is also is_function/1 for functions. inspect/1 (without IO.) returns the printable form of any value as a string, which is handy inside interpolation.

Common mistakes

  • Writing .5 or 5.: Elixir requires digits on both sides of the dot, 0.5 and 5.0.
  • Using single quotes for text: 'hello' is a charlist (a list of code points), not a string. Use double quotes.
  • Expecting 10 / 2 to give 5: it gives 5.0. Use div(10, 2) for an integer.

Test yourself

What does 10 / 4 return?

  1. 2
  2. 2.5
  3. 3
  4. An error

What is nil in Elixir?

  1. A null pointer
  2. The atom :nil
  3. The integer 0
  4. An empty list

Why can String.length(s) and byte_size(s) differ?

  1. Strings are stored in UTF-16
  2. Some characters take more than one byte in UTF-8
  3. byte_size counts the quotes
  4. They never differ