Follow us
Breaking
Software

Gleam provides type safety for BEAM and JavaScript developers

Gleam offers a type-safe alternative to Elixir and Elm by compiling to both the BEAM virtual machine and JavaScript. Created by Louis Pilfold, the language enables full-stack development using the Lustre framework and provides seamless interop with existing Erlang and Elixir code.

Share

Scalability and targets

Gleam reached version 1.0 two years ago, which means it is ready for production systems. The language runs on the Erlang virtual machine, a platform that powers systems like WhatsApp. Gleam also compiles to JavaScript, which allows code to run in browsers, on mobile devices, or on runtimes like Node or Bun. Because Gleam compiles to JavaScript, a team writing a backend web API in Gleam can choose to also write their website frontend in Gleam, sharing code between both platforms and enjoying the friendly and productive type-based programming style throughout their application stack. Gleam aims for a small surface area, easy to read, and expressive design. The language provides static analysis and compile-time guarantees similar to Elm, OCaml, and Rust. It uses immutable data structures with structural sharing. Concurrent access to state happens through actors or the Erlang in-memory key-value database, ETS. The concurrency system runs millions of tasks and scales because the garbage collector never stops the world. Gleam competes with Alpaca and Caramel. Caramel is based on OCaml and even forks the OCaml compiler to generate code.

Feature Specification
Concurrency Actor-based
Typing Static
Data Immutable
Targets BEAM, JavaScript

Ecosystem and interop

Developers pick Gleam over Elixir or Elm to obtain tighter static analysis while staying in the BEAM ecosystem. Louis Pilfold, the language creator, stated that most Gleam programmers do not use Elixir. They come from other ecosystems instead. If you already have much Elixir in production, you can stick with Elixir for its dynamic approach. Gleam provides zero-cost interoperation with Erlang, Elixir, and LFE. A Gleam build tool compiles Elixir dependencies and Elixir source files. Developers call external functions using a specific syntax. This interop allows the introduction of Gleam into existing BEAM projects incrementally. Elixir uses a dynamic type system, whereas Gleam has a small surface area. One downside is the standard library being at version 0.69.0 while the language is at 1.0. Gleam also allows you to use Erlang hot code reloading, even if it lacks the extra guarantees Erlang provides. All existing Erlang and Elixir OTP modules are usable in Gleam. An HTTP server made with Gleam was once the fastest server available on the BEAM. Gleam even has registry modules to help with process management. Gleam uses the Hex package manager. The language provides actor-based multi-core concurrency and Erlang-style fault tolerance.

Lustre and web development

Lustre provides a web framework for Gleam that follows the Model-View-Update architecture. It supports single-page applications, client-side components, and server components. Teams build full-stack applications by using Gleam for the backend and Lustre for the frontend. Lustre components work as Web Components or run on the server with a runtime that sends patches to the DOM over WebSockets. Users use recursion for iteration because the language lacks for-loops. The compiler forces developers to cover all cases in a case expression. Developers also use the Mist HTTP server. The language server provides code completion and error detection in editors like Zed. Lustre includes companion libraries like Lustre UI for design tokens and Lustre SSG for static site generation. The architecture relies on a Model to represent state, a Message type to handle communication, and a View function to render the model to HTML. Lustre is an Elm-inspired framework that provides a declarative, functional API for constructing HTML. It uses the Effect type to handle things like HTTP requests or communication with the outside world. Developers can use the command to start a single-page application in the browser or start a server component anywhere Gleam runs. Does the ecosystem provide enough specialized libraries to replace every existing BEAM tool?

Share

Technewsdaily

Senior tech writer covering AI, gadgets and cybersecurity. Breaking down the news that matters, every day.