README.md

Elixir

Elixir is a programming language built on top of Erlang. As Erlang, it is a functional language with strict evaluation, single assignment and dynamic typing built to support distributed, fault-tolerant, non-stop applications with hot swapping. Elixir allows you to invoke Erlang modules without a need to convert data types, therefore there is no hit in performance when invoking existing Erlang code.

The main difference between Elixir and Erlang is its syntax inspired by Ruby and method dispatching and metaprogramming on top of modules.

Usage

Elixir is still in development. If you want to help building it or are just looking for some fun, you can get started now! First, you need to clone this repository to your machine, compile and test it:

Elixir requires Erlang R14B01 or later version to execute (R14A and R14B do not work). If you have the correct version and tests still fail, feel free to open an issue in the issues tracker on Github. If all tests pass, you are ready to play with Elixir!

This README provides a length explanation about Elixir in the Learning Elixir section below. There are also some examples in the examples folder that you can run by executing the bin/elixir EXAMPLE and an interactive Elixir available as bin/iex. Feel free to build your own examples and study the language better.

Contributing & Roadmap

Currently, there is an effort to improve Elixir Standard Library. As much of Elixir's STDLIB is written in Elixir and tested in Elixir, you don't need to be an advanced Erlang user to improve the language, just know the OTP a bit. As an example, you may take a look at the List implementation and its tests to check how simple it is.

If you want to contribute to Elixir, the code is organized as follow:

include, src - Both directories contain the part of the source code written in Erlang. leex and yecc were used as tokenizer and parser respectively;

lib - Contains Elixir's STDLIB, written in Elixir;

test/elixir - Tests for Elixir's STDLIB, written in Elixir. For this purpose, Elixir ships with a small unit test library called ExUnit;

test/erlang - Contains tests for Elixir, written in Erlang. Usually, just internal stuff is tested here. The preferred way to test is in Elixir itself.

If you are interested, check out the ROADMAP.md file in the repository or keep reading this README to find items to be implemented.

Important links

Learning Elixir

This is a basic introduction into Elixir. Some sections have a paragraph called "To be implemented", they represent parts of Elixir that was not implemented yet and that are under discussion.

This introduction borrowed its guidelines from Learn You Some Erlang, a great resource to learn Erlang which will be referenced several times during this introduction.

Hello World

Let's start with a simple hello world. The first step is to create a new file called "hello.ex" inside Elixir repository with the following contents:

module Hello
def world
IO.puts "Hello World"
end
end

Now, we can compile this file to the current directory:

bin/elixirc hello.ex -o .

Notice that a .beam file was added to the current directory with the compiled code. We can execute it by invoking the method world in the module Hello in the same directory:

bin/elixir -e "Hello.world"

And you will see "Hello World" printed! This example works because Elixir automatically loads the compiled files in the current directory. If your compiled files are in other directories, you can pass those new directories to bin/elixir using -pa and -pz as options. Type bin/elixir with no arguments for more information.

When you are building libraries in Elixir, those are the main steps you should take. Write your code, compile it and run it! However, sometimes it is nice to just put some code together and run it, without a explicit compilation step. For that, elixir allows you to easily create scripts. Let's create a new file "hello.exs" with the following contents:

IO.puts "Hello World"

And now run it:

bin/elixir hello.exs

And it works again! Notice we used the extension .exs instead of .ex here. This is just a convention, Elixir does not treat .exs files differently from .ex files in any way! In fact, you could even try to compile the .exs file:

bin/elixirc hello.exs -o .

When you do that, you can see that "Hello World" is printed as well. This is because Elixir actually executes the files to compile them. This is the key to many Elixir features, as we are going to see later.

Also notice that Elixir ships with an interactive console that you can use for most examples in this tutorial, you can start it with:

bin/iex

Enjoy!

Some notation

Before we start, notice that comments in Elixir are, as in Erlang, done with %.

% This is a commented line

Throughout this introduction, % => represents the result of an expression:

To be implemented

Currently, there is no support to enter numbers in bases other than base 10. This is the current API in Erlang (although the best API for Elixir is under discussion):

2#101010. % => 42
8#0677. % => 447
16#AE. % => 174

Atoms

Elixir also has Atoms, called Symbols in other languages like Ruby. Although its syntax was borrowed from Lisp:

'atom
'Atom
'atom_without_spaces

Atoms are literals, with their own value as name. An atom 'symbol is an atom 'symbol everywhere, with exactly the same value. Atoms start with a single quote and should not have spaces (spaces delimit the atom end). Atoms with spaces are represented by wrapping them in quotes:

'"Atom with Spaces"

As in Erlang and Ruby, Atoms are not garbage collected, so remember to not generate atoms dynamically, otherwise you will run out of memory sooner rather than later.

Documentation:

Booleans

As in Erlang, the boolean values are simply atoms named true and false. However, to avoid writing 'true and 'false, Elixir also allows you to simply write true or false. The following are all equivalent and will yield 1 as result:

if 'true
1
else
2
end
if true
1
else
2
end
if 'false
2
else
1
end
if false
2
else
1
end

Besides those two boolean values, Elixir also has a nil value which is simply an atom as well. nil also evaluates to false in conditionals.

Tuples

Tuples are used to organize many terms together when you know how many terms there are. As in Erlang, a tuple is written in the following form:

% A tuple containing all boolean values
{ true, false }
% A tuple that may represent a point with coordinates X and Y
{ 10, 20 }
% An empty tuple
Tuple.new

Tuples and lists (which are going to see next), are zero-indexed in Elixir while they are one-indexed in Erlang. You can retrieve a specific element using []:

Lists

Elixir Standard Library has a bunch of methods to interact with lists:

[1, 2, 3].length % => 3
['a, 'b, 'c][1] % => 'b

As in Elixir + is simply a method like any other (and not an arithmetic operator as in Erlang), it can also be used to add arrays:

[1, 2, 3] + [4, 5, 6] % => [1,2,3,4,5,6]

Lists in Erlang and Elixir are implemented as linked lists. This means prepending an item to the list is quite fast, but appending is much slower. Therefore we have a special syntax to prepend one or more items to a list:

Ordered Dicts are recommended to deal with small amount of data. Other data structures are recommended to deal with a huge amount and you can read more about others key-value store, but remember that most of them are not implemented in Elixir yet.

Strings

This is expensive because each character uses 8 bytes of memory, not 8 bits! Erlang stores each character as a 32-bit integer, with a 32-bit pointer for the next item in the list.

Elixir takes a different approach to strings. Strings in Elixir are handled as UTF-8 binaries. Since a binary is nothing more than a bit string, where the number of bits is a multiple of 8, we can create strings using the bit string syntax:

<<72, 73, 74>> % => "HIJ"

When a bit string with multiple of 8 bits is created, it is automatically mapped to a string. However, you will rarely use the syntax above as Elixir provides the more traditional quote syntax to handle strings:

Keep in mind that, as Elixir strings are different from Erlang strings, sometimes you may need to convert Elixir strings to a char list and vice-versa when invoking Erlang methods using the methods to_char_list and to_bin as seen above.

Besides using call, we can also invoke functions using apply, brackets [] and the .() syntax:

my_function = -> (x, y) x + y
my_function.(1, 2) % => 3

Another useful extension Elixir adds to functions is the easy generation of anonymous functions. For instance, suppose you have a list of cars and you want to get their names. A way to do that would be:

cars.map -> (c) c.name

However, you can easily generate an anonymous functions that does the same:

cars.map _.name

Anonymous functions can also be generated with arguments, so the map expressions we saw above:

[1,2,3].map -> (x) x * 2 % => [2,4,6]

Could actually be rewritten as:

[1,2,3].map _.*(2)

Currently, functions do not support partial applications or pipes, but such features will be added down the road.

Elixir will often complain if you bound a value to a variable but never use it. For instance, imagine that you want to get just the first element of a tuple with three items:

{x, y, z} = {1, 2, 3}

If you don't use the y and z variables, Elixir will show you some warnings. For this reason, you could use _ instead:

{x, _, _} = {1, 2, 3}

The variable _ is always unbound:

_ = 1
_ % => Raises that variable '_' is unbound

Sometimes having several occurrences of _ in the same expression is confusing, so you can do this instead:

{x, _y, _z} = {1, 2, 3}

The values 2 and 3 will be bound to the variables _y and _z, but Elixir won't complain if you eventually don't use them.

Keep in mind that the number of expressions allowed in pattern matching are limited. You cannot invoke methods, use interpolated strings, retrieve constants and so on. Therefore, this is invalid:

1.abs = -1

Ordered dicts are also allowed in pattern matching but there is one important restriction: you are responsible to make their order match. Therefore, this won't work:

dict = { 2: 4, 1: 2 }
{ 2: 4, 1: 2 } = dict

This fails because the dict variable is ordered, so it is actually represented as {1: 2, 2: 4}. Remember that OrderedDicts are ordered according to Elixir ordering of terms and not the order new items are added. This ordering rule is important to allow us to bound variables to key-values:

Lists are compared element by element. Tuples are ordered by size, two tuples with the same size are compared element by element. If one of the compared terms is an integer and the other a float, the integer is first converted into a float, unless the operator is one of === and !==.

All term comparison operators return a boolean expression.

Arithmetic operators

Operator

Description

Argument

+

unary +

number

-

unary -

number

+

anything

-

anything

*

anything

/

returns a float

anything

div

returns an integer

anything

rem

returns an integer

anything

Except by the two unary operators, all other operators can be overloaded. For instance, we can concatenate two lists by using the + operator:

[1,2,3] + [4,5,6] % => [1,2,3,4,5,6]

This is the same as:

[1,2,3].+([4,5,6]) % => [1,2,3,4,5,6]

Notice however that we cannot add a list with a number:

[1,2,3] + 1 % => Raises an error

Also, Elixir keeps the same semantics as Erlang in the sense the / operator always returns a float when numbers are given as argument. The div and rem operators are used to deal with integers:

2 / 1 % => 2.0
6 div 4 % => 1
6 rem 4 % => 2

Bitwise operators

To be implemented/written.

Strict boolean operators

Elixir provides the following operators to deal strictly with booleans:

Operator

Erlang equivalent

Description

and

and

Both expressions must return boolean

or

or

Both expressions must return boolean

andalso

andalso

First expression must return boolean, short-circuit operator

orelse

orelse

First expression must return boolean, short-circuit operator

not

not

Unary operators, expression must be a boolean

Logical operators and control-flow

Elixir provides three operators general purposes operators:

Operator

Description

&&

and

||

or

!

not

Remember that everything, except false and nil, evaluates to true:

!false % => true
!true % => false
!Object.new % => false

Both && and || are actually control structures. They do not return a boolean but the last evaluated expression:

Similar to the match syntax, you can catch different values in the same clause:

try
self.throw {1,2}
catch {1,2}, {3,4}
IO.puts "Rescued a tuple"
end

In order to catch an error or an exit, you need to be explicit:

try
self.error {1,2}
catch {1,2}
IO.puts "I will never get a tuple {1,2}"
catch 'error: {1,2}
IO.puts "Rescue an error with {1,2}"
end

You must use the keyword after if you want to execute some code regardless if there was an exception or not:

try
self.error {1,2}
catch {1,2}
IO.puts "I will never get a tuple {1,2}"
after
IO.puts "I am always executed"
end

It is important to keep in mind that tail calls are not optimized inside try blocks. This is expected as the runtime needs to keep the backtrace in case an exception occur. Also, notice that variables created inside try/catch/after clauses do not leak to the outer scope.

List of errors

Here is a list of runtime errors that can be raised by Elixir:

{ 'builtin_not_allowed, { method, builtin } }

Invoking method not allowed on the builtin structure. Builtins are all structures that comes directly from Erlang, they are: String, Integer, Float, Tuple, List, OrderedDict and so forth. Binding and setting instance variables are currently disabled on builtins;

{ 'module_defined, { name, file, line } }

An module with name was already defined on file at line. This is a common error to appear during compilation time as the following valid Ruby pattern is not valid in Elixir:

module Foo
module Bar
end
end
module Foo
module Baz
end
end

In the example above, we are reopening Foo to add a Baz module. This is invalid in Elixir as modules cannot be reopened. To handle this, just define Baz directly as Foo::Baz:

module Foo::Baz
end

{ 'no_local_method, { name, arity, module } }

There isn't a local method with the given name and arity in module;

{ 'not_a_module, { method, other } }

method failed because other is not a module;

{ 'no_module, name }

A module with name could not be found;

{ 'no_callback, { name, arity, structure } }

The callback name with arity was not implemented in structure. Raised when a structure is given as callback but does not comply to all conditions;

{ 'bad_ivar, name }

The name given is not an atom and cannot be given as internal variable name;

{ 'bad_binding, { module, actual } }

Could not bind to module module as __bound__ callback returned actual;

{ 'bad_ivars, value }

value given to @() or set_ivars is not an OrderedDict or it is an OrderedDict but not all keys are atoms;

{ 'internal_method_overridden, { method, arity } }

The method with arity arity is defined automatically by Elixir and cannot be overridden.

All string sigils follow the same set of rules. They start with a ~ followed by a letter and the string is delimited by a separator. The available separators are (), [], {} and "". If the letter after ~ is lowercased, no interpolation is allowed, if uppercased, interpolation is allowed. A couple more examples:

Heredoc

string = ~~
This is a string which
preserves whitespace at
the beginning and also
handles #{'interpolation}
~~

Similar to Ruby, HEREDOCs allow an identifier right after the initial three quotes:

string = ~~HTML
<p>Nice!</p>
~~

This allows to identify the content and most text editor uses it to properly syntax highlight it. Besides, you can add Elixir code after the HEREDOC and they still are properly evaluated:

string = ~~STRING + "123"
abc
~~
string % => "abc\n123"

Consequently, this feature allows multiple HEREDOCs:

list = [~~ONE, ~~TWO, ~~THREE]
this is the first string
~~
this is another one
~~
this is the third. cool, isn't?
~~
list[0] % => "this is the first string\n"
list[1] % => "this is another one\n"
list[2] % => "this is the third. cool, isn't?\n"

Invoking Erlang Methods

Invoking Erlang methods with elixir is quite trivial:

% Accessing the is_atom BIF from Erlang.
% This is the same as `is_atom(foo)` in Erlang.
Erlang.is_atom('foo) % => true
% Accessing the function delete from module lists.
% This is the same as `lists:member(1, [1,2,3])` in Erlang.
Erlang.lists.member(1, [1,2,3]) % => true

As there is no conversion between most Erlang data types and Elixir ones, there is no performance hit in invoking Erlang methods. The only exception are strings that are binaries in Elixir and may need to be converted to char lists in some specific erlang modules. More details were outline in the BitString and String sections above.

Finally, notice that Erlang is just a proxy that is converted to erlang calls at compile time.

List and Bit string comprehensions

List comprehensions allow you to quickly build a list from another list:

[n*2 for n in [1,2,3,4]] % => [2,4,6,8]

The comprehension is defined with the for keyword which accepts several expressions. Those expressions can be generators, as in x in [1,2,3,4], or filters:

% A comprehension with a generator and a filter
[n for n in [1,2,3,4,5,6], X rem 2 == 0] % => [2,4,6]
% A comprehension with two generators
[x*y for x in [1,2], y in [2,3]] % => [2,3,4,6]

There are two types of generators in Elixir/Erlang: list and bit string generator:

Remember, as strings are binaries and a binary is a special kind of bit string where the number of bit is a multiple of 8, we can also use strings on comprehensions. For instance, the example below removes all white space characters from a string:

<<c for <<c>> in " hello world ", c != $\s>> % => "helloworld"

Elixir does its best to hide the differences between list and bit string generators from you. However, there is a special case due to Erlang limitation that you need to explicitly tell Erlang that a list is being given as argument:

% This will fail because when Elixir sees that the left side
% of the in expression is a bit string, it expects the right side
% to be a bit string as well:
[n*2 for <<n>> in [<<1>>,<<2>>,<<3>>] % => [2,4,6]
% You need to be explicit and use inlist:
[n*2 for <<n>> inlist [<<1>>,<<2>>,<<3>>] % => [2,4,6]
% inbin is also available:
[n*2 for <<n>> inbin <<1,2,3>>] % => [2,4,6]

Modules

Method Visibility

One important aspect of modules is the method visibility. Elixir provides two different visibilities: public and private. All methods are public by default, this means that a method can be called from anywhere, at any time:

Private methods are internal and therefore cannot be accessed in the self.method format:

module Example
def calling_private_method
private_method
end
def calling_private_method2
self.private_method
end
private
def private_method
13
end
end
% Won't work, the method is private.
Example.private_method
% It works because calling_private_method is calling private_method without self.
Example.calling_private_method % => 13
% It won't work because calling_private_method is calling private_method with self.
Example.calling_private_method2

Local and remote calls

In Erlang, it is very important to make a difference between local calls and remote calls, as they affect how hot code swapping works. You can read this section from Learn You Some Erlang for more information.

In Elixir, every time you call a private method, it is doing a local call. This means that private methods are always called locally (i.e. in the same module) and can't be overridden in mixins. Consider the following example:

The third fibonacci method in OptimizedMath is optimized because the last method calls itself. In order to understand the difference between both versions and how tail call optimization works, we recommend reading more about it on the Recursion chapter from Learn You Some Erlang.

Pattern matching in methods

As we mentioned earlier and saw in the examples above, pattern matching is also allowed in method signatures. If the given args does not match a given method, it will try the next one until it succeeds or none is found, raising an error. Below, is an example that checks if a list is the prefix of another, relying solely on pattern matching:

Documentation

Module binding, refinements and mixins

Elixir provides a way to bind modules to structures in order to provide method dispatching. A couple of features are built on top of this functionality and are going to be described next.

Binding

Binding is the ability to bind modules to data types. Method dispatching is done by simply invoking a method in the module bound to the structure. For example, when the method + is invoked in a string, it is simply invoking the method + defined in String::Behavior as below:

All built-in data types (like integers, tuples, lists, etc) are already bound to a given module at runtime and cannot be changed. However, Elixir provides a data structure called blank slate that is more flexible and can be bound to any module:

The Module.blank_slate expression above returns an empty data type that is then bound to the module Car using the operator #. Since the expression Module.blank_slate#Car() is too long, the form commonly used is:

car = #Car()
car.engine % => "VROOOM"

Notice the # operator has the same precedence as ., so the next expressions are equivalent and will all print "VROOOM":

Internal variables

Elixir allows us to store information inside blank slates. This is done with internal variables. In the example below, we are going to store the color of a car by using the __bound__ callback and then read it:

module Car
% Callback invoked whenever this module is bound to a structure.
% All the argument passed to the bind operator are accessible here.
def __bound__(color)
% Set the internal variable color to the given color.
@('color, color)
end
def color
% Read the internal variable @color.
% @internal_variables are always relative to self.
@color
end
end
car = #Car('green)
car.color % => 'green

Whenever a module is bound, the callback __bound__ in the module is invoked. All the values given on binding are accessible in the callback. Data can be added to the blank slate through internal variables (for example, @color above).

In the example above, set_ivar is setting the value of the internal variable @color but it is not returning a new object, it is modifying the value of the module in place! However, notice that modules are only mutable during definition. After a module is defined, we can no longer modify it:

Notice in the example above Car's internal variable @color is still 'red even after calling set_ivar. Immutability is important to ensure we won't have race conditions or deadlocks on runtime. However, it is ok for modules to be mutable as long as it happens only during compilation time, adding great extensibility.

Best practices

Libraries must hide the blank slate binding as most as possible. For instance, the Car example above, requires the developer to manually bind a method, leading to coupling. Ideally, the Car should provide an API for that as below:

Mixins

Mixins are the ability to mix one module into another module still in definition:

module SimpleMath
def one
1
end
def two
2
end
end
module AdvancedMath
mixin SimpleMath
def one_plus_two
one + two
end
end
AdvancedMath.one_plus_two % => 3

Different from refinements that only alter method lookup, mixins provide a copy mechanism that copy all the methods defined in the mixed in module into the target providing faster behavior at run-time.

Note that mixed in methods are available straight away, while methods defined in the module are just available after the module is defined:

module AdvancedMath
mixin SimpleMath
one % => 1
two % => 2
def one_plus_two
one + two
end
% Fails because AdvancedMath is still
% in definition.
one_plus_two % => ERROR
end

Finally, notice that methods that are not available at compile time cannot be called locally. For instance, imagine you have module RequiresName that requires name to be implemented in the target:

In the example above, RequiresName uses self.name instead of name. This is required because name is not known locally.

Temporary mixins

Sometimes it is also convenient to include a mixin only during the
module definition. This is achieved with temporary mixins:

module SimpleMath
def one
1
end
def two
2
end
end
module AdvancedMath
using SimpleMath
one + two % => 3
def one_plus_two
one + two
end
end
% Will fail because SimpleMath methods are
% available only during the module definition.
AdvancedMath.one_plus_two % => ERROR

Summary

Binding allows us to change dispatch on a given data type at runtime;

Refinements allows us to change dispatch on an existing module at compile time;

Mixins allows us to mix behavior from existing modules during module definition.

Documentation

Code and load paths

Loading code in Elixir happens by automatically loading modules inside the compilation directory. For instance, if you are building a library and have the compiled code inside the exbin/ directory, you can access any of the modules in it using:

bin/elixir -pa exbin/ -e "SomeCompiledModule.method"

You can find more documentation by typing "bin/elixir". You may also add and remove paths programatically

When scripting, it may be convenient to load another specific script file, you can do that using Code.load_file or Code.require_file in which the second assures the file is being loaded just once.

Documentation

Advanced Topics

Some advanced topics related to Elixir.

Variable scopes

As explained at the beginning of this README, Elixir allows the same variable to be assigned more than once. However, keep in mind that variables assignment inside functions do not change the original binding. For example:

a = 1
b = -> a = 2
b()
a % => 1

As everything is immutable, when the function assigns a new variable, it creates a new binding with the new variable value and the original binding is never modified. This is important to avoid side-effects when passing functions to different processes.

Also, Elixir has much more flexible rules when it comes to variables inside control-flow expressions. For instance, the following works:

x = 1
if true
x = 2
end
x % => 2

The same is also true for receive/after and case/match expressions. The only exception comes to try/catch scenarios, where a variable defined inside such blocks is never accessible from the outside. For example:

x = 1
try
x = 2
catch _:_
% Do nothing
end
x % => 1

Guards

Elixir has basic support for guards. They can be used on method declaration, receive/match clauses, case/match clauses and catch clauses. In all cases, they are declared using the keyword when. For instance, you could implement a method that returns the absolute value of a number as follow:

def abs(x) when x < 0
- x
end
def abs(x)
x
end

In a receive/case match clause, we would do instead:

case y
match x when x < 0 then - x
match x then x
end

Finally, in catch expressions it works as follow:

try
throw y
catch 'throw:x when x < 0
- x
catch 'throw:x
x
end

Guards only supports arithmetic operators on numbers, comparison operators and the following boolean operators: or, orelse, and, andalso and not.

Dynamic Dispatch and Metaprogramming

Elixir allows you to dynamically dispatch methods using send:

[1,2,3].send 'head % => 1
{}.send 'empty? % => true

Elixir also allows you to dynamically define methods. For example, below we can define attribute readers for both "title" and "author" attributes dynamically:

Documentation

License

Copyright (c) 2011 José Valim

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.