Follow us
Breaking
Tech News

Myths and facts of Gleam’s type-safe actor model adoption

Explore how Gleam implements type-safe actors on the BEAM using the gleamotp package. Learn how the language provides zero-cost interop with Elixir and JavaScript while maintaining production stability, as demonstrated by Strand's zero-crash currency exchange API.

Share

The static typing myth

The assertion that static typing prevents the implementation of type-safe actors on the BEAM remains a persistent myth. Some developers argue that OTP processes communicate via the actor model by sending messages of any type, which makes compile-time verification impossible. However, developers have created thin layers on top of OTP for both PureScript and Gleam that provide type-safe interfaces. Gleam uses the gleam_otp package to provide typed bindings for actors and supervisors, although this package remains experimental. This package provides fault tolerance and self-healing through supervisors but avoids certain specialized behaviors like GenServer or Registries to maintain type safety. I find the lack of support for all OTP system messages a major drawback for developers who require the full suite of Erlang debugging tools. Gleam implements primitives like statically typed actors instead of full specialized OTP behaviors to maintain its small surface area. Gleam 1.0 reached maturity with a guarantee of backward compatibility based on semantic versioning. Concurrent access to mutable state happens through actors or Erlang’s in-memory key-value database ETS. The core maintainer notes that Gleam’s concurrency system runs millions of tasks concurrently and scales easily because of immutable data and a garbage collector that never stops the world.

Interoperability and the JavaScript target

Gleam provides zero-cost interoperation with Elixir, Erlang, and LFE. The Gleam build tool compiles Elixir dependencies and source files, which allows a developer to mix Elixir and Gleam code within a single program for a unified development experience. This interop extends to the JavaScript runtime, where Gleam code runs on engines such as Google’s V8. A team writing a backend web API in Gleam can also write their website frontend in Gleam by using the Lustre framework. This approach allows for sharing code between both platforms while using the same type-based programming style throughout the application stack. The compiler also monomorphises record updates in version 1.7.0, which removes the runtime conditional logic cost for both Erlang and JavaScript targets. The gleam publish command checks for top-level namespace pollution to prevent collisions in the Erlang virtual machine’s single namespace. Gleam is the only language in the set of BEAM competitors, including Alpaca, Caramel, and Elixir, that also targets JavaScript. The language server provides type and documentation details when you hover over a variable. Does the current inability to type check upgraded code during Erlang hot code reloading limit the usefulness of this full-stack capability?

Deployment and the Rust comparison

Teams choosing between Elixir and Rust often struggle with the trade-off between dynamic flexibility and the expressivity of algebraic data types. Gleam provides sum and product types similar to Rust, though it lacks the steep learning curve found in the language. One developer completed the Gleam Language Tour in one evening to become productive. Gleam uses only 22 reserved keywords, and 15 are currently in use. Pattern matching and recursion are the primary methods for control flow. Strand, a London marketing agency, uses Gleam for its currency exchange rate API service. They report zero Gleam-related crashes in their production environment since they began. The BEAM ensures that if an external service fails, the individual process crashes without affecting the rest of the application. You should keep in mind that Gleam requires an Erlang installation to run on the BEAM and a suitable runtime like NodeJS to run on JavaScript. Concurrent access to mutable state happens through actors or Erlang’s in-memory key-value database ETS.

Share

Technewsdaily

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