O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Compiling a Module

Let’s look at two ways to compile this file and load it into iex. First, if you’re at the command line, you can do this:

 
$ ​iex times.exs​​
 
iex>​ Times.double 4
 
8

Give iex a source file’s name, and it compiles and loads the file before it displays a prompt.

If you’re already in iex, you can use the c helper to compile your file without returning to the command line.

 
iex>​ c ​"times.exs"
 
[Times]​​
 
iex>​ Times.double(4)
 
8​​
 
iex>​ Times.double(123)
 
246

The line c "times.exs" compiles your source file and loads it into iex. We then call the double function in the Times module a couple of times using Times.double.

What happens if we make our function fail by passing it a string rather than a number?

 
iex>​ Times.double(​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required