Is Gleam the Friendly Functional Language You’ve Been Waiting For?
This article introduces Gleam, a type‑safe functional language that compiles to Erlang and JavaScript, walks through installation, a hello‑world program, package management, pattern matching with algebraic data types, and demonstrates its concurrency‑friendly, null‑free design.
Gleam is a type‑safe functional programming language for building scalable concurrent systems. It compiles to Erlang and JavaScript, allowing direct inter‑operation with other BEAM languages such as Erlang and Elixir.
Erlang’s long‑standing focus on concurrency and fault‑tolerance makes it a solid foundation, and Gleam aims to be friendly without assuming prior knowledge.
Hello World
import gleam/io
pub fn main() {
io.println("hello world!")
}Installation on macOS can be done via Homebrew, which also installs Erlang automatically.
brew install gleamGleam provides a project generator similar to Rails; creating a new “hello” project yields a default hello.gleam file containing the hello‑world code.
Package Management
Two .toml files configure dependencies. In gleam.toml you declare required libraries, for example:
[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"Adding packages such as envoy (environment variables) and argv (command‑line arguments) lets you read an environment variable based on user input:
import argv
import envoy
import gleam/io
import gleam/result
pub fn main() {
case argv.load().arguments {
["get", name] -> get(name)
_ -> io.println("Usage: get <name>")
}
}
fn get(name: String) -> Nil {
let value = envoy.get(name) |> result.unwrap("")
io.println(format_pair(name, value))
}
fn format_pair(name: String, value: String) -> String {
name <> "=" <> value
}The case expression matches patterns, and the underscore acts as a catch‑all. Gleam’s pipe operator ( |>) improves readability of chained function calls.
Algebraic Data Types
Custom types can be defined and pattern‑matched, for example:
pub type Season {
Spring
Summer
Autumn
Winter
}
fn weather(season: Season) -> String {
case season {
Spring -> "Mild"
Summer -> "Hot"
Autumn -> "Windy"
Winter -> "Cold"
}
}Another example shows a sum type Travel with associated data:
pub type Travel {
Walk(hours: Int)
Cycle(hours: Int)
Drive(hours: Int, speed: Int)
}
pub fn main() {
let walking = Walk(1)
let cycling = Cycle(1)
let bus_trip = Drive(2, 50)
let trip = [walking, cycling, bus_trip]
io.debug(trip)
}Gleam has no null, no implicit conversions, and no exceptions; errors are handled with the built‑in Result type. Numeric operators are distinct for integers ( +) and floats ( +.).
Overall, Gleam offers a gentle entry point to functional programming while preserving the benefits of immutability and type safety.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
