Elixir Data Types

Welcome to a tutorial on Data Types in Elixir. Here you will learn about the 7 basic data types supported by the Elixir language, which are integers, floats, Booleans, atoms, strings, lists, and tuples.

 

Numerical Types

Just like other programming languages, Elixir supports both integers and floats. if enter any integer or float as input in your Elixir shell, it will return its value.

Example: 

18

if you run the program above, the output will be:

18

 

Also, numbers can be defined in octal, hex, and binary bases.

Octal

To define a number in octal base, use '0o' as the prefix. E.g. 0o52 in octal is equivalent to 42 in decimal.

 

Hexadecimal

To define a number in decimal base, use '0x' as the prefix. E.g. 0xF1 in hex is equivalent to 241 in decimal.

 

Binary

To define a number in binary base, use '0b' as the prefix. E.g. 0b1101 in binary is equivalent to 13 in decimal.

Elixir supports 64bit double precision for floating point numbers, and can as well be defined using an exponentiation style. E.g. 10145230000 can be written as 1.014523e10

 

Atoms

Atoms are constants whose name is their value and can be created using the color(:) symbol. This is shown below.

:hello

 

Booleans

Elixir supports true and false as Booleans. These values are typically attached to atoms :true and :false respectively.

 

Strings

In Elixir, strings are inserted between double quotes, and they are encoded in UTF-8. Also, they can take up multiple lines and contain interpolations. Thus, to define a string simply enter it in double quotes, as shown below.

"Hello world"

Now, for you to define multiline strings, we use a syntax similar to python with triple double quotes. This is shown below.

"""
Hello
World!
"""

More on strings are discussed in the String Tutorial.

 

Binaries

Binaries are simply sequences of bytes enclosed in << >> and separated with a comma. Check out the example below.

<< 65, 68, 75>>

Binaries are frequently used to handle bits and bytes-related data if there is a need. By default, they store 0 to 255 in each value. However, this size limit can be increased by using the size function that says how many bits it should take to store that value. check out the example below.

<<65, 255, 289::size(15)>>

 

Lists

Square brackets are used by Elixir to specify a list of values. These Values can be of any type. Check out the example below.

[1, "Hello", :an_atom, true]

But, lists come with inbuilt functions, as the head and tail of the list are named hd and tl, which return the head and tail of the list respectively. However, in some cases, when you create a list, it'll return a char list, because when elixir sees a list of printable ASCII characters, it prints it as a char list. 

Note that strings and char lists are not equal. This will be clarified in the subsequent tutorials.

 

Tuples

Curly brackets are used in Elixir to define tuples. Quite similar to lists, tuples can hold any value.

{ 1, "Hello", :an_atom, true }

There might be a contradicting view on why we provide both lists and tuples when they both work in the same way. But, the thing here is that they have different implementations such as:

  • Lists are stored as linked lists, therefore insertions, and deletions are very fast in lists.
  • Tuples are stored in a contiguous memory block, which makes accessing them faster but adds an additional difficulty to insertions and deletions.