Follow us
Breaking
Software

Gleam and the BEAM: A guide to type-safe actors and web development

Learn how Gleam provides type-safe APIs for the BEAM actor model through the experimental gleamotp package. This guide covers Gleam's core features, including the Lustre web framework and seamless interop with existing Erlang and Elixir libraries.

Share

Gleam V1 launched on March 4, 2024. The language compiles to Erlang and JavaScript. It uses sum types to express exclusivity and pattern matching to deconstruct values. Developers code by thinking about the data structure first and then the implementation. The Gleam compiler and LSP notify you if your pattern matching is not exhaustive, which forces developers to account for every possible constructor in a custom type before the code ever reaches a production environment. The Gleam CLI includes all necessary tools to prevent decision paralysis. It includes gleam format and gleam test to manage the project. Users manage dependencies with gleam add or use gleam lsp for language server support. The gleam check command performs type checking. The gleam shell command starts an Erlang shell.

Command Purpose
gleam add Add new project dependencies
gleam check Typecheck the project
gleam format Formats source code
gleam test Run the project tests
gleam lsp Run the language server

Gleam is type-safe. The LSP shows both the type and documentation for variables when you hover over them. When you hover over a constructor, the LSP shows the parent custom type name and the associated documentation. The compiler reduces runtime errors by validating code structure. This validates that the code matches the intended data structure. Gleam does not include macros, meta-programming, or traits.

The gleam_otp actor model

The gleam_otp package provides bindings for the BEAM actor framework. This package provides type-safe APIs for actors and messages. It also provides supervisors to create hierarchical process structures.
gleam_otp is experimental.
The library does not support named processes or all OTP system messages. Supervisors in this package do not yet support different shutdown periods for each child. Because OTP processes communicate via the actor model using messages of any type, implementing full static typing for all GenServer behaviors remains a challenge. The library currently lacks support for all OTP’s specialized behaviors like Registry, Agent, or Task.
The actor model works.
You know the basics of the BEAM, so focus on how Gleam tightens the logic. The actor is the most common process type in Gleam. It handles OTP system messages automatically to enable debugging and tracing. The process type is the fundamental building block for other actors. Tasks provide a way to compute values and return results to a parent. Supervisors start and then supervise a group of processes. They can restart processes if they crash or terminate them when the application shuts down. Supervisors can start other supervisors, creating a supervision tree.
How will Gleam handle the lack of named processes in the future?

Web development and interop

Lustre is an isomorphic web framework for Gleam. It runs on both the backend and the frontend. This framework draws inspiration from Elm. It is opinionated, so developers see the same conventions and code structure across different applications.
Lustre is opinionated.
Gleam allows developers to call Erlang and Elixir code via the @external keyword. This makes the language compatible with existing Hex libraries. The language also supports Erlang’s hot code reloading. The syntax uses snake_case for variable and number names.
Gleam is simple.
Gleam uses only 22 reserved keywords, and 15 of those are currently in use. It does not include if statements, for loops, or while loops. Users use case expressions and recursion for flow control. Recursion works efficiently because Gleam uses tail-call optimization. This optimization unrolls the recursive call into a loop if the last thing the function calls is itself. This gives recursion the same performance as a normal loop.

Share

Technewsdaily

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