Scripts2026年4月19日·1 分钟阅读

Gleam — Friendly Type-Safe Language for the BEAM

Gleam is a statically typed functional language that compiles to Erlang and JavaScript, combining type safety with the fault-tolerant BEAM runtime.

Introduction

Gleam is a statically typed functional programming language designed for building reliable systems on the Erlang BEAM virtual machine. It also compiles to JavaScript, letting you share code between server and browser. Gleam offers a friendly developer experience with helpful error messages and a consistent, small language surface.

What Gleam Does

  • Compiles to both Erlang and JavaScript from a single codebase
  • Provides full static type checking with type inference
  • Interoperates seamlessly with Erlang, Elixir, and OTP libraries
  • Ships with a built-in package manager, build tool, formatter, and LSP
  • Produces fault-tolerant applications using BEAM supervision trees

Architecture Overview

Gleam's compiler is written in Rust and parses .gleam source files into a typed AST. It then emits either Erlang source or JavaScript modules. On the BEAM side, the generated Erlang is compiled to bytecode and runs inside the standard OTP runtime, gaining access to lightweight processes, supervisors, and distributed messaging out of the box.

Self-Hosting & Configuration

  • Install via Homebrew (brew install gleam), Nix, or prebuilt binaries from GitHub releases
  • Requires Erlang/OTP 26+ for BEAM targets; Node.js 18+ for JavaScript targets
  • Project config lives in gleam.toml (dependencies, target, name)
  • Dependencies are fetched from the Hex package registry
  • Use gleam export erlang-shipment to produce a self-contained release

Key Features

  • Exhaustive pattern matching with compile-time guarantees
  • Use blocks for readable sequential transformations
  • First-class support for OTP behaviours like GenServer and Supervisor
  • Sub-second incremental compilation via the Rust-based compiler
  • Growing ecosystem with packages for HTTP, JSON, databases, and testing

Comparison with Similar Tools

  • Elixir — dynamic typing with optional Dialyzer specs; Gleam enforces types at compile time
  • Erlang — Gleam adds type safety while compiling to the same BEAM bytecode
  • Rust — targets native binaries with ownership model; Gleam targets BEAM/JS with GC
  • OCaml — similar ML-family type system but lacks BEAM concurrency primitives
  • PureScript — compiles to JS with strong types; Gleam also targets the BEAM

FAQ

Q: Can I use existing Erlang and Elixir libraries from Gleam? A: Yes. Gleam has a Foreign Function Interface for calling Erlang and Elixir code directly, and many Hex packages work out of the box.

Q: Is Gleam ready for production? A: Gleam reached 1.0 in March 2024 and is used in production by several companies. The language and standard library follow semantic versioning.

Q: How does Gleam handle concurrency? A: On the BEAM target, Gleam uses lightweight Erlang processes and OTP supervisors. On JavaScript, it uses promises and async runtimes.

Q: What editor support is available? A: Gleam ships an official Language Server Protocol (LSP) implementation providing autocompletion, go-to-definition, and inline error reporting in VS Code, Neovim, Helix, and other editors.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产