What is Elixir and where it runs
Elixir is a general-purpose programming language for the Erlang virtual machine. José Valim started it in 2011 with a clear goal: keep everything that makes Erlang good at running many things at once, and put a modern, pleasant language on top of it. It is functional, the data you pass around does not change, and the programs you write are ordinary text files that the Elixir toolchain compiles and runs.
Three ideas explain most of the language, and this page takes them one at a time: everything runs on the Erlang VM, a value never changes after it is built, and the unit of concurrency is a process that shares nothing and talks by messages. The rest of the course is those ideas in practice.
What Elixir is
A program is a set of modules, and a module holds named functions. A function takes values in and returns one value out; there is no return keyword, because the last expression of the body is the result. The little program below defines one module with one function, trims a name and prints the greeting it builds:
greet.exs
defmodule Greeting do def for(name), do: "Hello, " <> name <> "!" end name = " Ada " |> String.trim() IO.puts(Greeting.for(name))
elixir greet.exs
Hello, Ada!
String.trim() removes the spaces and the pipe sends its result into Greeting.for/1 as the first argument, so the line reads left to right. The <> operator joins two binaries, which is what a string is in Elixir. This is the whole loop of the course: write a file, run it with elixir, read the output.
The Erlang VM underneath
The Erlang virtual machine, usually called the BEAM, is the program that executes the compiled code. It was built for telephone switches, so it was designed for a machine that must keep answering while parts of it fail. It schedules its own lightweight processes, keeps each one isolated, and can run millions of them on one machine.
Elixir compiles to the same bytecode Erlang does, and the two languages call each other without a bridge. That is why an Elixir program can use an Erlang library directly, and why a company can add Elixir to a system that has run Erlang for years. The Erlang module :erlang is callable from Elixir today, and the next lessons use it whenever the standard library does not cover a case.
The VM is also where the famous uptime comes from: a process that crashes does not take the machine down, and a supervisor starts it again. The lesson after the install shows a supervisor restarting a crashed worker, and it is called The BEAM and OTP in one page; for now the point is that the language and the runtime agree on how failure is handled.
A value never changes
A map is a set of keys and values, written between %{ and }. The update syntax builds a new map and leaves the old one exactly as it was. Nothing is copied in place, nothing is mutated, and the old value is still there for anyone who holds it:
immutable.exs
person = %{name: "Ada", year: 1815} older = %{person | year: 1852} IO.inspect(person) IO.inspect(older)
elixir immutable.exs
%{name: "Ada", year: 1815}
%{name: "Ada", year: 1852}
The binding older holds a second map, and person keeps the first. This is not a rule the programmer has to remember, because there is no operation that could break it: the language has no assignment to a field. A function that wants to change a value returns a new one, and the caller decides what to do with it. Programs become easier to test, because a function cannot quietly change something a caller still uses.
The mistake: adding strings
The first mistake a reader from Ruby or JavaScript makes is to add two strings with +. The operator adds numbers, and the program stops with an ArithmeticError before it prints anything:
plus.exs
IO.puts("hello" + "world")
elixir plus.exs
** (ArithmeticError) bad argument in arithmetic expression: "hello" + "world"
:erlang.+("hello", "world")
plus.exs:1: (file)
Strings are joined with <>, as the first program of this page does. The error names the operator it tried, which is the fastest way to see which one a line used.
Processes talk by messages
A process is a small unit of work scheduled by the VM. It has its own memory and no way to read the memory of another process: the only way to reach it is to send it a message. spawn/1 starts one from a function, send/2 puts a message in its mailbox, and receive takes a message out:
processes.exs
parent = self() pid = spawn(fn -> receive do {:greet, name, from} -> send(from, {:hello, name}) end end) ref = Process.monitor(pid) send(pid, {:greet, "Ada", parent}) receive do {:hello, name} -> IO.puts("the process says hello to #{name}") end receive do {:DOWN, ^ref, :process, ^pid, _reason} -> IO.puts("process alive: #{Process.alive?(pid)}") end
elixir processes.exs
the process says hello to Ada
process alive: false
The spawned process receives one tuple, answers with another, and ends. Its pid is not printed, because a pid changes on every run. Process.monitor/1 asks the VM for a message when the process dies, which is how a program watches another one without sharing anything. The processes lesson builds on these three functions.
Where Elixir runs
Elixir runs wherever the BEAM runs: Linux, macOS, the BSDs, Windows and containers. The same code runs on all of them, and a release built on one machine runs on another with the same architecture. That makes it a real option for a web backend, a message broker, an embedded device and a command-line tool at the same time.
Teams pick it for systems that must stay up under load: chat and messaging platforms, telemetry pipelines, payment APIs and IoT gateways. The reason is not a benchmark but the shape of the runtime: many small processes, a supervisor for each group, and a crash that costs one process rather than the service. A Docker image with the toolchain is the fastest way to try it without touching the machine you are on.
This course starts with the toolchain, then the basics of the language, and reaches the runtime and OTP once the syntax is familiar. The next lesson installs Elixir on the system you have.
The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which runtime executes an Elixir program?
Answer
The BEAM, the virtual machine of Erlang
Elixir compiles to the bytecode of the Erlang VM, and the BEAM executes it.
-
Who created Elixir, and on top of what?
Answer
José Valim, on top of the Erlang virtual machine
José Valim built Elixir in 2011 as a modern language for the Erlang VM.
-
What does it mean that values in Elixir are immutable?
Answer
A function returns a new value and the old one stays as it was
There is no operation that changes a value in place: a change is a new value.
-
What is a process on the BEAM?
Answer
A lightweight unit of work with its own memory that talks by messages
The VM schedules processes itself; they share nothing and reach each other only by messages.
-
What does this program print?
IO.puts(String.upcase("beam"))Answer
BEAM
String.upcase/1 returns the upper-case binary and IO.puts/1 prints it.
-
What does this program print?
name = "Ada" IO.puts("Hello, " <> name)Answer
Hello, Ada
The name holds the binary "Ada", and <> joins it to the greeting without adding a space or a mark.
-
What does this program print?
person = %{name: "Ada"} %{person | name: "Grace"} IO.inspect(person)Answer
%{name: "Ada"}
The update builds a second map; the binding person still holds the first one.
-
What does this program print?
parent = self() pid = spawn(fn -> send(parent, :done) end) receive do :done -> IO.puts("got it") end IO.puts(is_pid(pid))Answer
got it / true
The child sends :done, the parent prints got it, and spawn/1 returned a pid.
-
What does this program do?
IO.puts("hello" + "world")Answer
An ArithmeticError, because + adds numbers and <> joins strings
The + operator accepts numbers only; two binaries are joined with <>.
-
What does this program do?
person = %{name: "Ada"} %{person | age: 36}Answer
A KeyError, because the update syntax needs a key that is already there
The update syntax replaces existing keys only; adding a key needs Map.put/3.
Автор статьи: Arthur Isaev