🎓 A1Learner
🔍
0 of 54 lessons completed

Elixir Basic Types

A data type is the kind of a value, such as a whole number or a piece of text. Elixir has a small set of basic types that cover numbers, truth values, and text.

Checks name the type of a number

The is_integer check answers true for a whole number. The is_float check answers true for a decimal number and the last check answers false.

count = 42
price = 3.5
IO.puts(is_integer(count))
IO.puts(is_float(price))
IO.puts(is_integer(price))

is_binary checks for text

A string is stored as a binary. The check answers false for a number.

name = "Ana"
IO.puts(is_binary(name))
IO.puts(is_binary(42))

A charlist holds character codes

The ~c form builds a list of codes, one code per character. IO.puts prints those codes as the letters abc.

letters = ~c"abc"
IO.puts(letters)

Example

count = 12
price = 4.5
ready = true
name = "Ana"
IO.puts(count)
IO.puts(price)
IO.puts(ready)
IO.puts(name)
IO.puts(is_integer(count))
IO.puts(is_binary(name))

Key points

  • Integer: a whole number with no decimal point, such as 42.
  • Float: a number with a decimal point, such as 3.5.
  • Boolean: a truth value that is either true or false.
  • String: text written inside double quotes, such as "Ana".
  • Binary: the form that holds a string, so is_binary checks whether a value is text.
  • Charlist: a list of character codes written with ~c, such as ~c"abc".

In this example, count holds an integer and price holds a float. The name ready holds the boolean true and name holds a string. The prints show each value in order, and the two checks at the end both answer true.

Note

a charlist prints as plain letters under IO.puts, even though it holds a code for each character.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which check answers true for the value "Ana"?