DEV Community

Carlos Ramírez
Carlos Ramírez

Posted on • Edited on

Elixir data types Part 1

Introduction:

When it comes to programming in Elixir, understanding the different data types is crucial. We'll cover the main data types in Elixir and how to work with them effectively. In this first part, we'll explore atoms and booleans, two of the simplest yet most fundamental data types in Elixir.

Atoms

Atoms are constants that represent names or values. They start with a colon (:) followed by a sequence of alphanumeric characters, underscores, or @. For example, :hello, :ok, :user_id, and :@name are all valid atoms.

Atoms are useful for naming things, such as keys in maps or messages in processes. Atoms are also used to represent the result of some operations, such as true and false for boolean expressions, or :error and :ok for error handling.

Atoms are also very efficient, as they are internally represented by an integer and are globally unique. This means that two atoms with the same name will always refer to the same value, regardless of where they are defined or used.

At first this data type may seem not useful, but in future posts you will sure see how powerful this data type is.

Booleans

Booleans are a special kind of atom that can only have two possible values: true and false. Booleans are used to represent logical conditions, such as comparisons or predicates.

Booleans can be combined with logical operators, such as and, or, and not, to form more complex expressions. For example:

true and false # false
true or false # true
not true # false
Enter fullscreen mode Exit fullscreen mode

Booleans can also be used in control flow constructs (more of this in a next post), such as if, case, and cond, to execute different branches of code based on some condition. For example:

if true do
  "Positive"
else
  "Negative"
end
Enter fullscreen mode Exit fullscreen mode

Summary

In this article, we learned about two of the basic data types in Elixir: atoms and booleans. We saw how atoms are constants that represent names or values, and how booleans are a special kind of atom that represent logical conditions. We also saw how atoms and booleans can be used in various ways in Elixir programs.

I hope this article was helpful for you. If you want to learn more about Elixir data types, you can check out these sources:

Basic types - The Elixir programming language
Typespecs and behaviours - The Elixir programming language
Elixir - Data Types
Typespecs — Elixir v1.12.3


Thanks for reading! My goal is to make Elixir accessible and easy to understand for everyone. If you have any questions or feedback, please don't hesitate to reach out. And be sure to check back regularly for more articles on https://dev.to/calbertora, as I strive to make each post as clear and readable as possible.

Top comments (0)