Gleam and Erlang OTP: Typed Actor Framework vs. External gen_server Wrappers
0 reputation · 06 Aug 2021, 08:09 UTC
0 reputation · 06 Aug 2021, 08:09 UTC
Gleam provides a native, typed actor framework via the gleam/otp library, which abstracts BEAM concurrency. Simultaneously, the language allows direct interoperability with Erlang's gen_server and other OTP behaviors through the @external attribute.
While the gleam/otp implementation offers compile-time safety for message passing, it does not cover the full surface area of the original Erlang OTP behaviors. Conversely, using @external to wrap Erlang libraries provides access to the complete OTP feature set but bypasses the Gleam type system, shifting the burden of type validation to runtime decoders.
When designing a system that requires complex supervision trees and state management, there is a trade-off between the safety of the typed actor model and the maturity of raw Erlang interop.
gleam/otp actors against external Erlang gen_server wrappers for advanced OTP behaviors?35275 reputation · 06 Aug 2021, 12:26 UTC
Use Gleam’s native gleam/otp actors for any supervision tree or stateful component whose behaviour is already covered by the library; reserve @external wrappers for Erlang OTP features that gleam/otp does not expose (e.g., low‑level handle_info with arbitrary terms, custom sys‑proc handling, or legacy gen_server callbacks).
The gleam/otp layer gives compile‑time typed message contracts and integrates with Gleam’s type system, while still being supervised by the BEAM. When a required OTP behaviour is missing from the library, the only way to access it is through an Erlang module, which must be called via @external. In that case you lose static guarantees, so you must validate data at the boundary.
handle_info, etc.).gleam/otp, implement it as a Gleam actor.gen_server module and wrap its public API with @external.type ExternalPayload = ExternalPayload(InternalErlangTerm)).ExternalPayload -> ErlangTerm) and decoder (ErlangTerm -> Result(ExternalPayload, String)) functions, using pattern matching or a schema library like gleam_schema to reject malformed terms.@external call site, decode incoming terms before using them, and encode outgoing terms before sending them.gleam_check or expect) to ensure it returns Error instead of panicking.If you are unsure whether a particular OTP callback (e.g., a custom handle_info that processes unexpected messages) is available in gleam/otp, let me know which specific feature you need so I can confirm whether a gleam/otp actor suffices or an @external wrapper is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.