2.1 Data Types
Elixir has many data types that will be familiar from other languages, as well as some that are particular to Elixir. In this lesson we'll look at the most important.
1.Introduction3 lessons, 11:49
2.The Elixir Language5 lessons, 30:55
3.Concurrency in Elixir6 lessons, 35:27
4.Conclusion1 lesson, 01:35
2.1 Data Types
Hi, and welcome back to Get Started With Elixir. In this lesson, we are going to learn all about data types in Elixir. There are a few that aren't common in other languages. Let's start with the base types. They all inherit from the underlying Erlang language. There are two types of numbers in Elixir. Integers don't have decimals and floats do. If you have programs in Ruby you might be familiar with symbols. There is an equivalent of them in Elixir called an atom. Atoms are very simple, their name is their value. You can even use them with quotes and spaces. There are a few atoms that are appropriated to match commonly used names. For example, true, false, or nil. You can leave the colon in these cases. The next one is binary. They represent the series of zeros and ones or bits. They are often grouped as eight bit pairs, bytes and are represented by numbers. Now we are in a territory you'll probably know, we have strings. While we are talking about strings it's worth to note that you can also use multiline strings by using the triple quotes syntax. When you have a group of values of different types you can create a tuple. Here I created a person two tuple that contains my name and my age. You can access the elements of a tuple by using the elem function. And you can update an element by using put_elem. Well, not really update, because of the general immutability, but you can create a new tuple of the new values. Let's continue to a more advanced type, maps. You will see them a lot. They are a simple key-value store, like a hash or dictionary in other languages. Here is an example, a map that defines a course. It has keys here, name and author with some values. You can access them either by using the dot syntax like course.name, or an array-like syntax with the author atom in the square brackets. You can also use strings as keys, although you lose the ability to use the dot syntax to look up items in the map. And by the way, binaries and strings are fully unicode compatible in Elixir. Of course, there are also array-like structures in Elixir. Here they are called lists. You simply draw in elements together to a list. They don't even need to be of the same type. The list type is to implemented as a linked list. This means that elements at the end are more expensive to access than the ones in the front. Because each element only knows about the next element and you need to iterate through all elements when you need the last one. This also means, and here even more so, that it's generally better to prepend an element to a list than to append it. If you append an element to a list, every item before it needs to be updated as well because elements are immutable, but they need to reference to the new element behind them. There is also special case. If you store a list of numbers that can all be represented as ASCII characters, Elixir automatically uses a character list for presentation. This is common in Erlang but not so much in Elixir, but you should be aware of it when dealing with Erlang libraries. Belive it or not, put functions are also basic types or first-class values in Elixir. You can set a variable to a function by using the fn keyword. And an error with a corresponding end to designate the function body. Since functions are such an important element of Elixir, we are going to go into more detail in the next lesson. Let's talk about high level types, they're a combination of base types. Keyword lists are a list of tuples that have an atom as a key and a value. Elixir has some convenient syntax to access the values of a keyword list easily. Although it looks a lot like a map, it is a different construct underneath so be aware of that. While we are talking about maps, let's talk about structs. They are special kinds of maps with a __struct__ key that defines the name of the struct. Let's get back to our course example. A course struct can contain name and the author of a course. But internally, they're just a simple map. Finally, we have the range type, which is also just a struct with a first and a last key. Of course, there is some nicer syntax for it in Elixir, like you're used to in other languages. I think for now, those are the most important data types you need to know about. There are a few more, but we will learn about them in later lessons. To recap, numbers can be integers or floats. Strings are just binaries. Tuples are used for grouping values together. A map is a key-value store type. Lists are collections of values and it matters if you are prepending or appending. Functions are first-class values in Elixir. And structs are maps with a special __struct__ key. In the next lesson, we are going to talk about modules and functions. See you there.