Languages / Language
Gleam
Erlang's reasons, ML's types: where two lanes of the tree meet.
Gleam is where two lanes of the lineage graph meet. From Erlang it takes the BEAM, the virtual machine Erlang runs on, and with it the reasons Erlang was built at all. From ML it takes static types that the compiler works out for itself. Louis Pilfold started it in 2016, the first numbered release (v0.1) came out in April 2019, and v1.0, the first stable release, followed on 4 March 2024. The name rhymes with, and means much the same as, “beam”.
From Erlang: the reasons
Erlang was built at Ericsson in 1986 for telephone exchanges, which must never stop: hundreds of thousands of calls at once, hardware failing underneath, software upgraded while it runs. Its answers were radical, and Gleam inherits every one of them by running on the same machine.
- Isolated processes. Millions of tiny processes, each with its own memory, that share nothing and talk only by sending messages (The Actor Model).
- Let it crash. Don’t defend against every error. Let the process die, cleanly and alone (see Failure).
- Supervisors. Another process watches, and restarts the dead one into a
known-good state. In Gleam these come from the
gleam_otplibrary, as typed actors and supervisors.
Gleam keeps the split Erlang draws between kinds of failure. An error you
expect, like a number that won’t parse, is a value: a Result you must
handle. Something that should never happen can still panic, and the crash is
the supervisor’s business.
From ML: the types
What Erlang never built in is a static type system: its types are checked as the program runs, and the Dialyzer tool finds some mistakes ahead of time, if you run it. Gleam brings the ML family’s answer:
- Types you mostly don’t write. Hindley-Milner inference: the compiler works the types out and checks them.
- Custom types and exhaustive Pattern Matching. A value is one of a
fixed set of shapes, and a
casethat forgets one will not compile. - No null, no exceptions. Something that can fail returns
Ok(value)orError(reason), and the compiler makes you deal with both.
The rest is taste. Pipes (|>) pass a value along a chain of functions,
so code reads in the order things happen. The language is small on
purpose: no macros, no type classes, not even an if or a loop, and the
v1.0 announcement says it can be learnt in an afternoon. One gleam binary is
the compiler, build tool, package manager, formatter and language server.
The honest comparison
Gleam is not Erlang’s only heir, or its closest. Elixir, started by José Valim in 2012, is the more direct successor in spirit: dynamically typed like Erlang, with a friendlier syntax, macros and tooling, and it brought the BEAM to a whole new crowd. (It has since begun adding a type system of its own.) Gleam is the typed one: it bets that Erlang’s runtime and ML’s compiler belong together.
Where it runs
Gleam compiles to Erlang and runs on the BEAM, where it can call Erlang and Elixir code and use their packages from Hex. Since v0.16 it can also compile to JavaScript, so the same code can run in a browser.
The compiler has its own small history. It began as experiments in Elixir, moved to Erlang, and was then rewritten in Rust, because refactoring type inference without static types of his own had become, in Pilfold’s words, a slow and error-prone process. The v1.0 announcement names Elm, OCaml and Rust as inspirations for the type system.
The people
- Louis Pilfold is a web programmer by trade whose hobby, for years, was writing compilers: before Gleam he wrote Dogma, a linter for Elixir, and exfmt, an Elixir formatter. As a child he spent so much time at the computer that his mother sent him to theatre classes, and he acted through his teens. He learnt compilers by building them, and has said Gleam started as a learning exercise that became useful almost by accident. Gleam is supported by sponsors, companies and individual users alike.
What made it different is that friendliness is a design goal, not an afterthought. The error messages explain themselves, the language stays small enough to hold in your head, and the home page calls it “a friendly language for building type-safe systems that scale.”
Landmark open-source projects
- Gleam: the compiler and toolchain.
- gleam_stdlib: the standard library.
- gleam_otp: typed actors and supervisors on Erlang’s OTP.
- Lustre: a web framework with an Erlang and Elm-inspired architecture.
- Wisp: a web framework for the BEAM.
Try it
import gleam/int
import gleam/io
import gleam/list
// int.parse can fail, so it returns a Result. Match on both cases.
pub fn main() {
["1986", "2024", "soon"]
|> list.map(int.parse)
|> list.each(fn(result) {
case result {
Ok(year) -> io.println("a year: " <> int.to_string(year))
Error(Nil) -> io.println("not a year")
}
})
}
Sources
- Armstrong, “Making Reliable Distributed Systems in the Presence of Software Errors” (2003 PhD thesis): why Erlang exists, which is why Gleam runs where it does.
- Pilfold, “Hello, Gleam!” (2019): the v0.1 announcement.
- Pilfold, “Gleam version 1” (2024): the first stable release, and what it promises.
- LambdaClass, “An interview with the creator of Gleam” (2019): ML on the BEAM, and why the compiler moved to Rust.
- Serokell, “Interview with Louis Pilfold” (2021): the person behind it.
- Gleam, “Frequently asked questions”: the name, the targets, Elixir, and what the language leaves out on purpose.
See also: Erlang, ML, Failure, Pattern Matching, The Actor Model.