A short introduction to types
Like most programming languages, Elixir has its fair share of numerical, boolean, character, and collection types. It also has some extra types, namely, atoms and binaries. In this chapter, we will see how all of these types work. However, let's start our discussion with numerical types.
Numerical types
Numerical types include the obvious integers. For example, in the interactive prompt (iex
), we can enter a few basic numbers:
iex(1)> 42 42
We can also do some basic arithmetic with numbers, of course:
iex(2)> 42 + 5 47 iex(3)> 6 * 7 42 iex(4)> 42 - 10 32 iex(5) 42 / 6 7.0
So, addition, subtraction, and multiplication work as we expect. Division, however, did what is typically called implicit type widening or implicit type casting. That is, we took two integer types and converted it into a floating type through division. In fact, the /
operator will always return a floating point type. If you want an integer type back, you can use the div
and rem
functions...