Module Names: Elixir, Erlang, and Atoms

When we write modules in Elixir, they have names such as String or PhotoAlbum. We call functions in them using calls such as String.length("abc").

What’s happening here is subtle. Internally, module names are just atoms. When you write a name starting with an uppercase letter, such as IO, Elixir converts it internally into an atom of the same name with Elixir. prepended. So IO becomes Elixir.IO and Dog becomes Elixir.Dog.

 iex>​ is_atom IO
 true
 iex>​ to_string IO
 "Elixir.IO"
 iex>​ ​:"Elixir.IO"​ === IO
 true

So a call to a function in a module is really an atom followed by a dot followed by the function name. And, indeed, we can call functions like this:

 iex>​ IO.puts 123
 123
 :ok
 iex>​ ​ ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.