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 called Elixir.IO.

 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
 iex>​ ​:"Elixir.IO"​.puts 123
 123

Get Programming Elixir 1.3 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.