Getting Started with Elixir - Data Types

Data types are the most basic building blocks of any language. In order to become proficient at building Phoenix applications, it's important to understand how Elixir works. So let's get started with Data Types. We'll be using IEx to explore some of the data types, so ensure that you have Elixir installed correctly.

Data types are the most basic building blocks of any language.  In order to become proficient at building Phoenix applications, it's important to understand how Elixir works. So let's get started with Data Types.  

We'll be using IEx to explore some of the data types, so ensure that you have Elixir installed correctly.

Data Types

Elixir has some common data types and others which have unique characteristics.

Here's a list of the available data types:

Integers & Floats

Like most languages, numbers are pretty straightforward in Elixir.  

When working with floats, anything between -1 and 1 must be start with a zero: 0.213, 0.54.

Also, the division operator / will always return a float.  Try it out in IEx:

Booleans

Again, pretty straightforward, booleans simply represent either a true or false value:

Strings

In Elixir, strings are stored as UTF-8 encoded binaries where each character is stored as its Unicode bytes. They are only enclosed in double quotes: "a string".  Read more about Elixir Strings in the docs.

An important distinction about strings is that anything wrapped in single quotes is a charlist: 'a charlist' which we'll cover next.

Some examples of working with Strings:

Notice that strings can be concatenated using <>.

Charlists

A charlist is a special kind of list of ASCII number values that represent each character rather than the characters themselves.

We can use i() to inspect a charlist in IEx:

You can see that the Raw representation is a list of the number values for each character.

If you enter the raw list in IEx:

You'll get the charlist value.

This is important to understand because if you need to return a list value with integers, Elixir can interpret it as the charlistvalue.  

For situations like these it's often more common to use a Tuple, which don't have the same issues as lists/charlists:

Atoms

Atoms are similar to symbols in other languages like Ruby: :atom.

They're constants and cannot change.  There really isn't much more to them.

You'll commonly see them used as return values to functions:

Enum.fetch returns a tuple where :ok is an atom and 2 is the value in position 1 from the zero-based list.

Lists

Lists are similar to arrays in other languages and are "lists" of items that may or may not be similar types.  They are known as "linked lists" meaning each item in the list has an internal pointer to the next item in the list.

Read more about (linked) lists in the docs.

They can contain mixed types and even other lists:

There is also a way to separate the head from the tail of a list where the head is the first item in the list, and the tail is everything after.  (Sometime known as the guillotine).

Tuples

Although tuples appear similar to lists, they are constructed differently under the hood and used differently in Elixir.  

Here's a great explanation from the docs:

Tuples are not meant to be used as a “collection” type (which is also suggested by the absence of an implementation of the Enumerable protocol for tuples): they’re mostly meant to be used as a fixed-size container for multiple elements. For example, tuples are often used to have functions return “enriched” values: a common pattern is for functions to return {:ok, value} for successful cases and {:error, reason} for unsuccessful cases.

Again, the fetch function can help demonstrate how they're commonly used:

They can also contain multiple data types:

Maps

Maps are key / value stores where each value is bound to a unique key.  Similar to Hash in Ruby or Dictionary in others.

Map.fetch can be used to fetch an items value by passing the key:

A few things to note about maps:

  • The order of the returned map may not be the same as the definition.
  • You can use string keys: %{"id" => 1}
  • If you mix string/atom keys, the atom must be last or a SyntaxError will be raised.

Ranges

A range represents a sequence of one or many, ascending or descending, consecutive integers.

Ranges can be either increasing (first <= last) or decreasing (first > last). Ranges are also always inclusive.

Conclusion

Thanks for reading the first article in this series!   We covered the basic data types of Elixir and used IEx to help demonstrate the concepts with a few examples.

Hit me up on Twitter @tmartin8080 if you have any questions or ideas.

Troy Martin
Senior Engineer working with Elixir and JS.
Keep in the Loop

Subscribe for Updates

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.